2016-12-15 67 views
2

我有一个球员,我使用下面的脚本旋转。它旋转播放器20度,而不是10(我需要10)。不明白为什么。当我按q时,它只执行一次。脚本旋转对象比我需要的更多

enter image description here

enter image description here

private UnityStandardAssets.Characters.FirstPerson.FirstPersonController firstPersonController; 
public GameObject player; 

void Start() 
{ 
    firstPersonController = player.GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController>(); 
} 

void Update() 
{ 
    StartCoroutine("RotatePlayerDelay");   
} 

IEnumerator RotatePlayerDelay() 
{ 
    Debug.Log("1"); 
    firstPersonController.m_MouseLook.myAngle += 10; // here I have +20, not +10 
    yield return new WaitForSeconds(0.005f); 
    firstPersonController.m_MouseLook.myAngle = 0; 
    Debug.Log("2"); 
} 

附:我需要协同程序,因为没有它,它将永远旋转

+1

这有点难以给出答案而不知道FirstPersonController类和MouseLook类。我也很确定你不需要一个协程,而是像“isAiming”或“isFacingTarget”这样的布尔值。顺便说一句:协程是开始每帧看起来不可取的。 – Wipster

回答

3

FixedUpdate不同,它每Time.fixedDeltaTime秒被调用一次,因此Update方法没有确切的时间步。它是一个依赖于游戏的FPS和所有渲染器和行为的持续时间并因此动态改变的协同程序。

你能想到的Update为:

void Start() 
{ 
    StartCoroutine(_update);   
} 
IEnumerator _update() 
{ 
    while(true) 
    { 
     Update(); 
     yield return null;    
    } 
} 

当你开始在Update方法的协同程序,你不能告诉前一帧是否已经结束。如果您的FPS低于1/0.005 = 200.0 FPS,那么协程的不同调用肯定会相互重叠。

0.005这里指的是产生回报新WaitForSeconds(0.005f)


尝试在另一个协程启动协程:

void Start() 
{ 
    StartCoroutine("RotatePlayerDelay");   
} 

IEnumerator RotatePlayerDelay() 
{ 
    while(true) 
    { 
     Debug.Log("1"); 
     firstPersonController.m_MouseLook.myAngle += 10; // here I have +20, not +10 
     yield return new WaitForSeconds(0.005f); 
     firstPersonController.m_MouseLook.myAngle = 0; 
     Debug.Log("2"); 
     yield return new WaitForSeconds(0.005f); 
    } 
}