[英]Unity, rotate an object in axis and back to starting point in the same direction
我想以恒定速度沿Y方向旋转对象。 停止后,我想朝同一方向旋转回Quaternion.identity。
public bool spin;
public float speed;
private void Update() {
if (spin) {
transform.Rotate (-Vector3.up, Time.deltaTime * speed, Space.World);
} else if (transform.rotaion != Quaternion.identity) {
transform.rotation = Quaternion.RotateTowards (transform.rotation, Quaternion.identity, Time.deltaTime * speed);
}
}
这很好用,但是却朝相反的方向旋转。 您如何迫使它保持向Quaternion.identity的原始方向旋转?
这很好用,但是却朝相反的方向旋转。
RotateTowards
将采用最短的路径到达其目标-可能是任何方向。
您如何迫使它保持向Quaternion.identity的原始方向旋转?
旋转超过180度(或中途)时的反向:
var dir = transform.eulerAngles.y < 180f ? 1f : -1f;
Quaternion.RotateTowards(rotation, Quaternion.identity, Time.deltaTime * speed * dir);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.