我试图根据用户输入(例如键盘左/右箭头)旋转一个对象(在一个轴中)。我使用的是Rigidbody和MoveRotation方法。
所以在FixedUpdate中,我使用Mathf.Lerp创建与帧无关的角运动。
rotation = Mathf.Lerp(rotation, rotation + input, Time.deltaTime * speed)但是这是线性的,我想要平滑这个旋转(例如Sine.easeIn),所以它开始慢慢旋转,过一段时间它就完全旋转了。
什么是统一的最佳方式,将推文与用户输入和帧独立结合起来。(我想我不能使用像DOTween或iTween这样的库,因为不知道吐温的时间。
发布于 2016-08-17 07:36:23
发布于 2016-08-17 15:10:10
所以它开始慢慢旋转,过了一段时间,它就完全旋转了。
这可以用一条协同线来完成。启动协同线并在while循环中完成所有操作。有一个变量,当按左/右箭头键时,它会随时间增加而使用Time.deltaTime。
public Rigidbody objectToRotate;
IEnumerator startRotating()
{
float startingSpeed = 10f;
const float maxSpeed = 400; //The max Speeed of startingSpeed
while (true)
{
while (Input.GetKey(KeyCode.LeftArrow))
{
if (startingSpeed < maxSpeed)
{
startingSpeed += Time.deltaTime;
}
Quaternion rotDir = Quaternion.Euler(new Vector3(0, 1, 0) * -(Time.deltaTime * startingSpeed));
objectToRotate.MoveRotation(objectToRotate.rotation * rotDir);
Debug.Log("Moving Left");
yield return null;
}
startingSpeed = 0;
while (Input.GetKey(KeyCode.RightArrow))
{
if (startingSpeed < maxSpeed)
{
startingSpeed += Time.deltaTime;
}
Quaternion rotDir = Quaternion.Euler(new Vector3(0, 1, 0) * (Time.deltaTime * startingSpeed));
objectToRotate.MoveRotation(objectToRotate.rotation * rotDir);
Debug.Log("Moving Right");
yield return null;
}
startingSpeed = 0;
yield return null;
}
}只需启动Start()函数一次的协同线即可。它应该永远运行。它将缓慢地旋转,直到到达maxSpeed为止。
void Start()
{
StartCoroutine(startRotating());
}您可以修改startingSpeed和maxSpeed变量以满足您的需要。如果您认为到达maxSpeed需要这么长时间,那么在向startingSpeed中添加Time.deltaTime;之后,总是可以用另一个数字乘以Time.deltaTime;。例如,将startingSpeed += Time.deltaTime;更改为startingSpeed += Time.deltaTime * 5;
https://stackoverflow.com/questions/38990444
复制相似问题