【问题标题】:Unity - Decrease/Increase the "Z axis" angle axis on Update()Unity - 在 Update() 上减少/增加“Z 轴”角度轴
【发布时间】:2015-10-15 18:12:22
【问题描述】:

[见下面我的回答!感谢 cmets]

在不使用“eulerAngles”的情况下减少/增加角度的最佳方法是什么?

根据我的“Y 轴”,我需要在 30 和 325 之间转换我的变换。

如果小于零,则减小“Z轴”的值,如果增加“Z轴”的值,则更大。

我已经试过了:

if ( airPlanePlayerRigidbody2D.velocity.y < 0 ) {
         var rotationVector = airPlanePlayerTransform.rotation;
         rotationVector.z -= 2f;
         Vector3 vec = new Vector3(rotationVector.x,rotationVector.y,rotationVector.z);
         airPlanePlayerTransform.rotation = Quaternion.Euler(vec);
 }

还有:

public void Rotate(float angle)
{
    airPlanePlayerTransform.Rotate(0, 0, angle); // this rotate forever
}

提前谢谢你。

更新

事实上,我已经有一个代码可以满足我的需要,但是它太长了。我正在寻找一些更简单的。我想要的结果是这样的:

更平滑的动画:Smooth animation down

到目前为止,我可以使用此处发布的 cmets 做些什么:Without animation

【问题讨论】:

    标签: c# unity3d transform euler-angles


    【解决方案1】:

    我认为 eulerAngeles 是一个很好的搭配方法,我在 Unity 中使用 eulerAngeles 成功地做了类似的旋转效果。你可能在使用的时候没有找到正确的方法。

    我用过类似的东西:

    Vector3 newRotationAngles = transform.rotation.eulerAngles;
    
        if (Input.GetKey(KeyCode.LeftArrow)) {
    
            newRotationAngles.z += 1;
    
        } else if (Input.GetKey(KeyCode.RightArrow)) {
    
            newRotationAngles.z -= 1;
        }
    
        transform.rotation = Quaternion.Euler (newRotationAngles);
    

    输出:当你按下左/右箭头键时,游戏对象会旋转,但它并不总是旋转。希望这会有所帮助。

    【讨论】:

    • 这看起来很有用,我会尝试实现必要的限制。看,我用结果更新了帖子。谢谢你的时间。
    【解决方案2】:

    你想要这样的东西:

    public float maxAngle = 325f; // Max Angle for Z Axis
    public float minAngle = 30f; // Min Angle for Z Axis
    public float rotationDelta = 1f; // you can control your rotation speed using this
    float tempAngle;
    void Update() 
    {
        // If AirPlane is rising, velocity in y is greater than 0
        if(rigidbody2D.velocity.y > 0)
        {
            tempAngle = Mathf.LerpAngle(transform.eulerAngles.z, maxAngle, Time.time * rotationDelta);
        }
        // If AirPlane is falling, velocity in y is less than 0
        else if(rigidbody2D.velocity.y < 0)
        {
            tempAngle = Mathf.LerpAngle(transform.eulerAngles.z, minAngle, Time.time * rotationDelta);
        }
        // If AirPlane is going straight in horizontal.
        else
        {
            tempAngle = 0;
        }
        transform.eulerAngles = new Vector3(0, 0, tempAngle);
    }
    

    【讨论】:

    • 有效!但他还是很“硬”,我需要更流畅的动画。看,我用结果更新了帖子。谢谢你的时间。
    【解决方案3】:

    研究主题,我设法得到了结果。感谢您的回复。

    if (airPlanePlayerRigidbody2D.velocity.y < 0) { //falling
           var myNewQuaternion = Quaternion.Euler (new Vector3 (0, 0, -30));
           var myQuaternion = Quaternion.Euler(new Vector3(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z));
           transform.rotation = Quaternion.Slerp (myQuaternion , myNewQuaternion ,  speed );
    }
    else if (airPlanePlayerRigidbody2D.velocity.y > 0) { // rising
           var myNewQuaternion = Quaternion.Euler (new Vector3 (0, 0, 30));
           var myQuaternion = Quaternion.Euler(new Vector3(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z));
           transform.rotation = Quaternion.Slerp (myQuaternion , myNewQuaternion ,  speed );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-25
      • 1970-01-01
      • 1970-01-01
      • 2019-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多