So I’m playing around with making a simple Pong game (with tutorials), and am trying to have it so the ball doesn’t move any faster after a certain point (it gets a bit faster each time it hits a paddle). Because if it moves too fast it just goes right through the paddles. I had wanted to add a couple extra features to make it my own even though I’m still new with coding.
Any help is greatly appreciated!
public class Ball : MonoBehaviour
{
public float speed = 200.0f;
private Rigidbody2D _rigidbody;
private void Awake()
{
_rigidbody = GetComponent<Rigidbody2D>();
}
private void Start ()
{
resetPosition();
AddStartingForce();
}
public void resetPosition()
{
_rigidbody.position = Vector2.zero;
_rigidbody.velocity = Vector2.zero;
}
public void AddStartingForce()
{
float x = Random.value < 0.5f ? -1.0f : 1.0f;
float y = Random.value < 0.5f ? Random.Range(-1.0f, -0.5f) : Random.Range(0.5f, 1.0f);
Vector2 direction = new Vector2(x, y);
_rigidbody.AddForce(direction * this.speed);
}
public void AddForce(Vector2 force)
{
_rigidbody.AddForce(force);
}
}