2016-04-15 177 views
0

我想旋转一组对象在90度的统一,所以我将所有对象设置为相同的父级,然后旋转父级,它旋转得很好但速度太快,我看不到,我怎么能减慢速度?我尝试都下面码的和两者是相同的:\此代码出现在由更新调用函数时上的按钮在Unity中旋转一组对象的速度太快

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

感谢在提前!

+0

发帖代码片段**完全没用**。没有人对此代码出现的位置有任何线索等。 – Fattie

+0

@JoeBlow编辑,希望现在更清楚。 – msLangdon95

回答

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左右。

+0

你是绝对正确的,我把它变成了一个if,并且让这个因子变大了,我只是想让它旋转90度,并且尝试了很多因素,而且它们都不是正确的:\ – msLangdon95

+0

编辑了一个如何夹住旋转的例子。 –

0

答案的这部分不起作用。请参阅更新

其实你做错了。 Unity中的动作应该通过更新慢慢完成,而不是一次。像ChristophKn一样,我建议使用协程。

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)。这将消除父母,但保持世界的地位。

  • 最后,将父母的旋转设置为原始值。

希望这会有所帮助。

+0

感谢您的回答,我试过了,它根本没有旋转:(! – msLangdon95

+0

它甚至没有进入while循环,我在循环之前和循环中添加了一个print语句,但没有达到它的内部 – msLangdon95

+0

好的,我修好了,只是一个更大的标志 – DRKblade