[英]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.