2017-01-11 9 views
0

basicly開始時刻を指定します。 >移動前の位置を開始し、かつ - 更新とcuroutineせずに私は望んでいた動きがあったが、私の飛行機がに5秒を待つべきでmathf.Repeatを使用し、私は彼が<strong>B</strong>彼はに戻っに到達したとき、私は位置<strong></strong></strong> B <strong>に毎回、から具体的な動きをしたい飛行機を持っている私のプロジェクトで

public class pingPongPlane : MonoBehaviour { 

    public float MinX = -10.2f; // y position of start point 
    public float MaxX = 55f; // y position of end point 
    public float PingPongTime = 1f; // how much time to wait before reverse 
    public Rigidbody rb; // reference to the rigidbody 

    void Start() 
    { 
     StartCoroutine(ShowEvent()) ; 
    } 

    IEnumerator ShowEvent(){ 
     while (true) { 
      yield return new WaitForSeconds (4f); 
      //get a value between 0 and 1 
      float normalizedTime = Mathf.Repeat (Time.time, PingPongTime)/PingPongTime; 
      //then multiply it by the delta between start and end point, and add start point to the result 
      float xPosition = normalizedTime * (MaxX - MinX) + MinX; 
      //finally update position using rigidbody 
      rb.MovePosition (new Vector3 (xPosition, rb.position.y, rb.position.z)); 
     } 
    } 
} 

は、だから私はこれをしませんでした私は他のソリューションを記憶していないし、次のコードで、クルーチンを使用して、その動きは滑らかではない、それはちょうど1つの位置から別の動きには見えませんちょっと奇妙です。

答えて

0

あなたがUpdateであなたの運動を行うが、動作していない

public bool CanStart = false; 

void Start() 
{ 
    StartCoroutine (StartMove()); 
} 

void Update() 
{ 
    if (CanStart == true) 
    { 
      //get a value between 0 and 1 
      float normalizedTime = Mathf.Repeat (Time.time, PingPongTime)/PingPongTime; 
      //then multiply it by the delta between start and end point, and add start point to the result 
      float xPosition = normalizedTime * (MaxX - MinX) + MinX; 
      //finally update position using rigidbody 
      rb.MovePosition (new Vector3 (xPosition, rb.position.y, rb.position.z)) 

    } 

} 

IEnumerator StartMove() 
{ 
    yield return new WaitForSeconds (5.0f); 
    CanStart = true; 
} 
+0

を必要な時間を待ってif声明とyieldと、数秒のためにそれを停止することができ、私は、オブジェクトを移動させたくありません消えるまで収縮したい、物体を動かす –