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!
\$\begingroup\$
\$\endgroup\$
1
-
\$\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 ♦Commented Jan 19, 2019 at 16:33
Add a comment
|
1 Answer
\$\begingroup\$
\$\endgroup\$
1
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
-
\$\begingroup\$ Simply put, if (x <=0 || x >= xmax){ xSpeed *= -1} etc. \$\endgroup\$– PeethorCommented Feb 18, 2019 at 20:11