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!

enter image description here

  • \$\begingroup\$ 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. ;) \$\endgroup\$ – DMGregory Jan 19 '19 at 16:33

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.