Unity - 减少/增加" Z轴" Update()上的角度轴

时间:2015-07-24 23:25:10

标签: c# unity3d transform euler-angles

[请参阅下面的回答!感谢您的评论]

在不使用" eulerAngles"降低/增加角度的最佳方法是什么?

根据我的" Y轴"我需要在30到325之间转换我的变换。

如果它小于零,我们减少" Z轴"的值。如果我们增加" Z轴"的价值,那就更大了。

enter image description here

我已经尝试过:

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

到目前为止,根据此处发布的评论我可以做的是: Without animation

3 个答案:

答案 0 :(得分:1)

我认为eulerAngeles是一个很好的方式,我通过使用eulerAngeles成功地在Unity中完成了类似的旋转效果。使用它时,您可能找不到正确的方法。

我使用了类似的东西:

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);

输出:当您按左/右箭头键时,gameObject将旋转,但它并不总是旋转。希望这有用。

答案 1 :(得分:1)

你想要这样的东西:

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);
}

答案 2 :(得分:0)

研究这个主题,我设法得到了结果。谢谢你的回复。

var customers = testing();
var oneCustomer = customers.FirstOrDefault(c => c.FirstName == "Thor");
// access oneCustomer.FirstName
相关问题