0
votes

I am working on a breakout style game in Unity using c# and wanted to know what is the best way to increase the speed of the ball over time without changing the angle/direction the ball travels after colliding with the paddle, so I want the ball to travel in the same direction/angle (say 45 degrees).

To start with I am using the simple code below which is attached to a box collider on the left of the paddle to get the ball to move left as desired. This gets the angle right but I wanted to increase the speed over time but not sure how to.

void OnCollisionEnter2D(Collision2D col) {

  if (col.gameObject.tag == "ball") {
       col.gameObject.GetComponent<Rigidbody2D> ().velocity = new Vector2 (-10f, col.gameObject.transform.position.y);
     }
} 
1
Are you wanting it to reach a max speed or just continually get faster? - Alan-Dean Simonds
Don't touch velocity, use AddForce instead and let physics figure out what happens next. To speed up over time use the ball's Update() method. E.g. if (isBallSpeeding) [ballGO's RB2D].AddForce(amount, type); - user2299169
continually get faster - Kaz
Actually I don't mind the ball either reaching a max speed or continually getting faster. My problem is controlling the direction to be constant. - Kaz

1 Answers

1
votes

I simply forgot to try the most basic solution which is to multiply the Vector by a speed variable, duh!. This does the intended behaviour I was after.

col.rigidbody.velocity = new Vector2 (-10f, col.gameObject.transform.position.y) * speed;