简体   繁体   English

统一,沿轴旋转对象并沿相同方向返回起点

[英]Unity, rotate an object in axis and back to starting point in the same direction

I want to rotate an object in the Y direction at constant speed. 我想以恒定速度沿Y方向旋转对象。 When stopped I want to rotate back to Quaternion.identity in the same direction. 停止后,我想朝同一方向旋转回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);
    }
}

This works great but it spins back on opposite direction. 这很好用,但是却朝相反的方向旋转。 How do you force it to keep spinning on original direction to Quaternion.identity? 您如何迫使它保持向Quaternion.identity的原始方向旋转?

This works great but it spins back on opposite direction. 这很好用,但是却朝相反的方向旋转。

RotateTowards will take the shortest path to its target - whichever direction that may be. RotateTowards将采用最短的路径到达其目标-可能是任何方向。

How do you force it to keep spinning on original direction to Quaternion.identity? 您如何迫使它保持向Quaternion.identity的原始方向旋转?

Reverse direction when rotation passes 180 degrees (or halfway): 旋转超过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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM