【Unity】3D-->角色控制-->移动转向方案

在Unity中通过输入行为控制角色转向

注:控制采用InputSystem,此处省略,传统输入同样适用

方案

存在以下3个可行方案

  1. 自行根据输入计算需要偏转的角度,且使用平滑过渡
  2. 使用Quaternion.LookRotation()方法,且使用平滑过渡
  3. 使用Transfrom.LookAt()方法,此方法不存在过渡
			{
				//自己计算转向
				float horizontalAngle = Mathf.Atan2(player.inputSystem.playerMovement.x, player.inputSystem.playerMovement.z) * Mathf.Rad2Deg;
				// float verticalAngle = Mathf.Atan2(player.inputSystem.playerMovement.y, new Vector2(player.inputSystem.playerMovement.x,player.inputSystem.playerMovement.z).magnitude) * Mathf.Rad2Deg;

				// Vector3 targetEulerAngles = new Vector3(verticalAngle, horizontalAngle, 0f);
				Vector3 targetEulerAngles = new Vector3(0f, horizontalAngle, 0f);
				player.transform.rotation = Quaternion.Slerp(player.transform.rotation, Quaternion.Euler(targetEulerAngles), Time.deltaTime * 10f);
			}

			//从inputSystem中获取值,需求中不存在Y轴的输入,故而Vector3中Y的值设置为0
			//Vector3 movement = new Vector3(player.inputSystem.playerMovement.x, 0f, player.inputSystem.playerMovement.z);

			{
				//使用Quaternion.LookRotation转向
				// player.transform.rotation = Quaternion.Slerp(player.transform.rotation, Quaternion.LookRotation(movement), Time.deltaTime * 10f);
			}

			{
				//LookAt直接转向,不存在过渡
				// player.transform.LookAt(player.transform.position + movement.normalized);
			}
			

效果

U3D平滑移动

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值