2016-11-29 8 views
0

オブジェクトを上向きに移動させようとしていますが、ランダムなジグザグの方向に移動しようとしています。オブジェクトを上に移動させるために次のコードを使用しました。オブジェクトを上に移動しますが、ランダムな最小の最大値で

transform.position + = transform.up * playerspeed * Time.deltaTime;

しかし、このオブジェクトを上向きに移動させるにはどうしたらよいですか?自分の最小値と最大値でジグザグの方向に移動します。ジグザグの経路を返すときはランダムですか?

+0

あなたが達成しようとしているパスの画像を描画することはできますか? 2つの値の間の振動で、オブジェクトに横方向の動きを加えようとしていますか?ジグザグのすべてのステップはランダムな長さですか?各セグメントの角度が同じでなければならないのでしょうか? (やはり、視覚援助はおそらく暗黙のうちにこれら全てに答えるだろう)。 – Serlite

答えて

1

あなたがする必要があるのは、xの位置を選んで上に移動するときに移動するだけです。それに達したら、そのプロセスを繰り返してください。

これを試してみてください:

private float minBoundaryX = -3f; 
private float maxBoundaryX = 3f; 
private float targetX; 
private float horSpeed = 3f; 
private float vertSpeed = 2f; 

//Pick a random position within our boundaries 
private void RollTargetX() 
{ 
    targetX = Random.Range(minBoundaryX, maxBoundaryX); 
} 

//Calculate the distance between the object and the x position we picked 
private float GetDistanceToTargetX() 
{ 
    return Mathf.Abs(targetX - transform.position.x); 
} 

private void Update() 
{ 
    //Roll a new target x if the distance between the player and the target is small enough 
    if (GetDistanceToTargetX() < 0.1f) 
     RollTargetX(); 
    //Get the direction (-1 or 1, left or right) to the target x position 
    float xDirection = Mathf.Sign(targetX - transform.position.x); 
    //Calculate the amount to move towards the x position 
    float xMovement = xDirection * Mathf.Min(horSpeed * Time.deltaTime, GetDistanceToTargetX()); 
    transform.position += new Vector3(xMovement, vertSpeed * Time.deltaTime); 
} 
関連する問題