- Home /
Setting the angle of a ball in a Breakout game
I'm having problems getting the ball to bounce off the paddle at the same speed, no matter the angle of deflection. It is my code, which I wrote quickly, but I'm not sure how to get it to work the way I want to.
Usually, whenever I'm writing a Breakout clone as a learning project, I set up a deflection table which allows me to tweak the angles and speed at will. However, on this occasion I've decided to try and get the ball to bounce at an angle determined by where it lands on the Paddle, and for it to calculate the angle on the fly rather than look for a pre-determined angle in the lookup table.
The simple code I have at the moment looks like this.
void BallHitPaddle()
{
GameObject paddle = GameObject.Find("Player");
float paddleX = paddle.transform.position.x;
float ballX = gameObject.transform.position.x;
float offsetX = ballX - paddleX;
gameObject.rigidbody2D.velocity = new Vector2((offsetX * speed * 2), speed);
}
Of course, this doesn't give the result I'm after. Although it deflects at nice angles, the acuter the angle, the faster the ball travels. I did find this: Angle and Velocity in a Projectile which looks like it could set me on the right path. Unfortunately I can't get it to work in C#. The compiler's throwing up all kinds of issues and I can't figure out a way around it.
Answer by NoodleSpenny · Mar 09, 2014 at 03:38 PM
Your ball's velocity vector describes both its speed and its direction. The speed of the ball will be the magnitude of its velocity vector, i.e. sqrt(velocity.x^2 + velocity.y^2)
. You can see that the way you are setting the velocity now, the magnitude (speed) will be different depending on where the ball hits the paddle.
What you want to do is determine the direction of travel first as a unit vector (a unit vector has a magnitude of 1). Then you multiply that direction by your desired speed, and that is what you will set your ball's velocity to.
float speed = 5.0f;
void BallHitPaddle()
{
GameObject paddle = GameObject.Find("Player");
Vector2 paddlePosition = paddle.transform.position;
Vector2 ballPosition = gameObject.transform.position;
// this is the vector 'pointing' from the paddle to the ball
Vector2 delta = ballPosition - paddlePosition;
// normalizing converts a vector into a unit vector
// (i.e. a vector with the same direction, but a magnitude of 1)
Vector2 direction = delta.normalized;
// set the velocity to be the direction vector scaled to the desired speed
rigidbody2D.velocity = direction * speed;
}
Thanks for this info. I am new to Unity and game dev in general. Started out creating the simplest game I could think of, handball. I used a pong tutorial I found as a base project and built on that. I had things set up pretty good, but the ball movement was too predictable. I wrestled for a couple of days with how to get the ball direction to behave based on where it hit the paddle. Finally found this and it helped so much. Now I need to get my head around how it works so I will understand it.
Answer by robertbu · Mar 09, 2014 at 03:23 PM
Try this
gameObject.rigidbody2D.velocity = new Vector2((offsetX * 2), 1).normalized * speed;