I programmed a little pong clone. At the start, the ball spawns in the middle of the field.
Expected output: I want the ball to fly in 45 degree angles to the upper right and left, and lower right and left randomly.
Real output: The ball only flies to the upper right and lower left.
Code:
public class BallController : MonoBehaviour {
public float speed;
private Rigidbody2D rb2d;
private int counter;
void Start() {
rb2d = GetComponent<Rigidbody2D> ();
SetStartPosition();
}
public void SetStartPosition() {
this.transform.position = new Vector3 (395f, 295f, 0f);
int xVector = GetRandom ();
int yVector = GetRandom ();
float x = (float) xVector;
float y = (float) yVector;
Vector2 movement = new Vector2 (x, y);
rb2d.velocity = movement * speed;
}
private int GetRandom() {
int i = 0;
do {
Random.InitState((int) System.DateTime.Now.Ticks);
i = Random.Range (-1, 2);
} while (i == 0);
return i;
}
}
Aucun commentaire:
Enregistrer un commentaire