So I got 2 problems, both in the same script. Im making a pong game at the moment, a game where you bounce a ball between two lines. And therefor I cant find the way to solve this:
Question 1: I need to make the ball fly to diffrent sides randomly, right now it only goes to the left, why isn´t my code working? :c
Question 2: The ball slows down every time it hit´s something, making the game very boring, is there a way to make it stay at the same speed all the time? Or increasing the speed more and more would work too
Code:
#pragma strict
var ballSpeed : float = 100;
function Start () {
yield WaitForSeconds (2);
GoBall ();
}
function OnCollisionEnter2D (colInfo : Collision2D) {
if (colInfo.collider.tag == "Player")
{
rigidbody2D.velocity.y = rigidbody2D.velocity.y/2 + colInfo.collider.rigidbody2D.velocity.y/3;
audio.pitch = Random.Range(0.5f,1.5f);
audio.Play();
}
else (colInfo.collider.tag == "Player 2");
{
rigidbody2D.velocity.y = rigidbody2D.velocity.y/2 + colInfo.collider.rigidbody2D.velocity.y/3;
audio.pitch = Random.Range(0.5f,1.5f);
audio.Play();
}
}
function ResetBall () {
rigidbody2D.velocity.y = 0;
rigidbody2D.velocity.x = 0;
transform.position.x = 0;
transform.position.y = 0;
yield WaitForSeconds (0.5);
GoBall();
}
function GoBall () {
var randomNumber = Random.Range(1f, 2f);
if (randomNumber <= 1f) {
rigidbody2D.AddForce (new Vector2 (ballSpeed, 10));
Debug.Log("shoot left");
}
else
rigidbody2D.AddForce (new Vector2 (ballSpeed, -10));
Debug.Log("shoot left");
}