赞
踩
问题案例,当需要播放翻滚动画的,先要旋转当前对象,而旋转的方向,就取决于我们的输入方向和相机的方向,然后再播放翻滚的动画.
当然也可以用作普通的输入方向和相机方向,将对象向着指定的方向进行移动,这样的话 _targetRotation 是直接进行使用,而不是用协程缓存的方式了.
简单测试
直接上代码
- using System.Collections;
- using UnityEngine;
- using UnityEngine.InputSystem;
-
- public class RotateTest : MonoBehaviour
- {
- public Vector2 inputMove;
- public bool toTargetFlag;
- public bool addCameraEuler;//是否加上相机的旋转角
- public float _tospeed = 5f;//旋转速度
- public float eulerAngle;//监视物体的欧拉角
- [Header("rotate calculate")]
- float _targetRotation;//目标旋转角
-
- Transform cam;
-
- void Start()
- {
- cam = Camera.main.transform;
- }
-
- void Update()
- {
- if (inputMove != Vector2.zero)
- {
- inputMove.Normalize();
- //计算输入的角度
- _targetRotation = Mathf.Atan2(inputMove.x, inputMove.y) * Mathf.Rad2Deg;
- if (addCameraEuler)
- {
- _targetRotation += cam.transform.eulerAngles.y; //只需要实现输入旋转时 屏蔽掉这行 ,
- }
- }
-
- if (inputMove != Vector2.zero && !toTargetFlag)
- {
- toTargetFlag = true;
- StartCoroutine(Rote());
- }
-
- eulerAngle = transform.eulerAngles.y;
- }
- IEnumerator Rote()
- {
- Quaternion cache = Quaternion.Euler(0, _targetRotation, 0);//需要到达的旋转方向
- while (transform.eulerAngles.y != cache.eulerAngles.y )
- {
- transform.rotation = Quaternion.RotateTowards(transform.rotation, cache, _tospeed);
- yield return null;
- }
- toTargetFlag = false;
- Debug.Log("我到达旋转的方向了");
- yield return null;
- }
-
- public void Move_performed(InputAction.CallbackContext obj)
- {
- //使用的是inputsystem的回调 ,换成 旧的input.getaxis("veritcal") ,input.getaxis("horizontal") 也行
- inputMove = obj.ReadValue<Vector2>();
- }
-
-
- }

其实这是很基础的东西了 - -!,希望对你能有帮助.
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。