在Unity中旋转一组对象太快了

时间:2016-04-15 13:07:14

标签: c# unity3d rotation

我试图将一组物体旋转90度,所以我将所有物体设置为同一个物体,然后旋转父物体,它旋转得很好,但它太快了我无法看到,我怎么能减慢速度呢?我尝试了下面的两个代码,两者都是相同的:\这个代码出现在用户按下按钮时由update调用的函数

    float totalRotation = 0;
    while (Mathf.Abs(totalRotation) < 90){
        totalRotation += Time.deltaTime;
        Parent.transform.RotateAround(temp.transform.position, flag*Vector3.right, Time.deltaTime);
    }

和这一个

    Parent.transform.RotateAround(temp.transform.position, flag*Vector3.right, 90f);

提前感谢!

2 个答案:

答案 0 :(得分:0)

使用角度因子,例如

Parent.transform.RotateAround(temp.transform.position, flag*Vector3.right, Time.deltaTime * 0.5f);

基于while,我猜你实际上并没有在Update中这样做。无论哪种方式,这都不起作用,因为更新是一个帧。您不希望while做任何与此相关的时间。请改为while if。这样旋转可能会太慢,因此要使因子更大。目前您的轮换是即时的。

编辑:
像这样的东西会起作用:

public bool isRotating = false;
public float speed = 20.0f;

public Vector3 targetRotation;

void Update()
{
    if(isRotating)
    {
        if(Vector3.Distance(transform.eulerAngles, targetRotation) > 1.0f)
        {
            transform.Rotate(Vector3.up * Time.deltaTime * speed);
        } 
        else
        {
            transform.eulerAngles = targetRotation;
            isRotating = false; 
        }
    }
}

这只会是y。

答案 1 :(得分:0)

这部分答案并不奏效。请参阅更新

其实你是以错误的方式做到的。 Unity中的移动应该通过更新缓慢完成,而不仅仅是一次。像ChristophKn一样,我建议使用Coroutine。

const float speed = 180;
IEnumerator Rotate () {
    float rotated = 0;
    while (rotated < 90) {
        float rotation = speed*Time.fixedDeltaTime;
        rotated += rotation;
        Parent.transform.RotateAround(temp.transform.position, flag*Vector3.right, rotation);
        //wait for the next fixed update to continue.
        yield return new WaitForFixedUpdate ();
    }
}

要开始旋转,请拨打StartCoroutine(Rotate);

修改

我有另一个想法,不是使用脚本而是动画:

  • 首先,向父对象添加旋转动画,旋转所需的角度。

  • 要旋转对象组,请像在问题中一样将它们设置为该父对象,然后启动动画。

  • 在动画结束时(您可以使用animation events调用您的方法),为组中的所有对象调用SetParent(null,true)。这将删除父母,但保持世界地位。

  • 最后,将您父母的轮换设置为原始值。

希望这会有所帮助。