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