- Home /
object reflecting of a wall
hello this question might have been asked before but previous questions have not helped me. basically i have a pong game. i have the ball moving in a 45 degree angle towards a wall on the top and bottom. i would like for it to hit the wall and bounce of at 45 degrees in the opposite direction as you would expect 2d pong game to behave but i have tried many different methods including, vector3.reflect and bouncy physic materials
thanks in advance for all answers
Answer by Graham-Dunnett · Jun 06, 2011 at 03:02 PM
Assuming that x is along and y is up, and you have a velocity vector that you use to move the ball, then I think you can just do:
vel.y = -vel.y
Do this when the ball hits the top or bottom wall. This basically says if the ball was moving upwards it now moves downwards, and vice versa. The movement of the ball in the left/right direction does not change.
The Vector3.Reflect function solves this problem in 3 dimensions. You did not say what problem you have with this function. For Pong it's probably overkill, though.
Note that you do need to attach a rigidbody to your ball for this to work.
i put this in OnTriggerEnter and visual studio says "vel does not exist in current context"
sorry to bring this up again but i still have no solution to my problem and i dont want to re-ask the question
Ins$$anonymous$$d of vely = -vel.y, try:
rigidbody.velocity.y *= -1;