2017-03-05 3 views
0

私はUnityにプレハブのインスタンスを複数持っています。各インスタンスは)(この更新プログラムを持っている:UnityのUpdate()は奇妙な動作をします

void Update() 
{ 
    Vector3 retning1200 = new Vector3(0, 0, 1); 
    Vector3 retning0130 = new Vector3(1, 0, 1); 

    Debug.DrawRay(transform.position, retning1200, Color.green, 1); 
    Debug.DrawRay(transform.position, retning0130, Color.green, 1); 


    if (Physics.Raycast(transform.position, retning1200, 1)) 
    { 
     antalNaboer++; 
    } 

    if (Physics.Raycast(transform.position, retning0130, 1)) 
    { 
     antalNaboer++; 
    } 

    Debug.Log("Myre i position: " + transform.position.x + " " + transform.position.y); 
    Debug.Log("antal naboer :" + antalNaboer.ToString()); 

    antalNaboer = 0; 
} 

私はUpdate()内のコードは、各フレームに1回実行されることを期待しています。しかし、すべてのインスタンスが実行されるとは限りません。 1つのフレームで複数回表示されることがあります。そして結果(アンタル・マイラー)は同じではありません。

Myre I位置:-1 0 アンタル隣人ネクストドア:2

Myre I位置:-1 0 アンタル隣人ネクストドア:1

Myre I位置:1 0 アンタル隣人ネクストドア:2

答えて

3

FixedUpdateメソッド内で物理的計算をチェックする必要があります。 名前は、この方法は、一定の時間期間で呼び出される示唆しているので、2つの連続する呼の間の時間期間は、常にこの詳細については、FPS

void FixedUpdate() 
{ 
    Vector3 retning1200 = new Vector3(0, 0, 1); 
    Vector3 retning0130 = new Vector3(1, 0, 1); 

    Debug.DrawRay(transform.position, retning1200, Color.green, 1); 
    Debug.DrawRay(transform.position, retning0130, Color.green, 1); 


    if (Physics.Raycast(transform.position, retning1200, 1)) 
    { 
     antalNaboer++; 
    } 

    if (Physics.Raycast(transform.position, retning0130, 1)) 
    { 
     antalNaboer++; 
    } 

    Debug.Log("Myre i position: " + transform.position.x + " " + transform.position.y); 
    Debug.Log("antal naboer :" + antalNaboer.ToString()); 

    antalNaboer = 0; 
} 

に応じて変えることができる同じとは異なり、Updateメソッドであるよう2人の違いはこのリンクをチェックしてください http://answers.unity3d.com/questions/10993/whats-the-difference-between-update-and-fixedupdat.html