# How can the ball moving like billiard after collision with wall?

I tried ball.velocity = Vector3.forward * speed and also *=-1 when the collision is occurred(It only works for straight way. But I want the ball movement be liked in photo. How can i do it? Thank you very much!

• Don't forget to search for similar questions first, to see if you can find existing answers without waiting for folks to type up new ones. Trust me, you're not the first game developer in history to ask about bouncing or reflecting a velocity off of a wall. ;) – DMGregory Jan 19 '19 at 16:33

## 1 Answer

If you have the velocity vector v and the normal vector n (the vector pointing in the direction the wall is pointing too), then it's simply

$$\vec v_2=\vec v-2 \vec n (\vec n \cdot \vec v)$$

Where v2 is the new direction vector

You could think of this as "sliding" the end of the velocity vector twice as much as it's facing in the wall's direction or by negating one of it's components

• Simply put, if (x <=0 || x >= xmax){ xSpeed *= -1} etc. – Peethor Feb 18 '19 at 20:11