So, I'm following a youtube tutorial on how to make pong. I understand and learned alot of stuff but there's a point where he made the ball and its movement but he didn't explain it. He used
void Start()
{
rb.velocty = new Vector2(speed, speed);
}
The ball is also attached with a rigidbody2d, a circle collider with a physic material attached. The physic material has a friction of 0 and bounciness to 1. This somehow made the ball bounce off an object.
Can anyone explain to me how it works?
Thanks alot!
The line
rb.velocity = new Vector2(speed, speed);
simply sets the initial velocity of the object in x
and y
direction.
Since they use x = y = speed
in the vector definition the ball will move diagonal in a 45° angle.
Change those values in order to get different angle or speed.
See Physic Materials:
friction = 0
means that the object will not slow down. A value bigger than 0
would slow down the object over time.
bounciness 1
means that on collision the object won't loose any speed but instead bounce back with 100% of its velocity at the according angle. -> This is the part changing the direction of the object according to its impact angle etc
This is all handled by Unity's PhysicsEngine itself so you don't have to worry about that.