0

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);
     }
} 
Kaz
  • 137
  • 2
  • 3
  • 17
  • Are you wanting it to reach a max speed or just continually get faster? – Alan-Dean Simonds Sep 21 '16 at 03:32
  • 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); –  Sep 21 '16 at 06:58
  • continually get faster – Kaz Sep 21 '16 at 08:54
  • 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 Sep 21 '16 at 13:04

1 Answers1

1

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;
Kaz
  • 137
  • 2
  • 3
  • 17