- Home /
Question by
RapsFan · Mar 22, 2012 at 10:05 PM ·
rigidbodybouncemathf.clamp
Air hockey game - getting puck to keep moving on change of direction
Hello everyone. I'm making an air hockey game and am having trouble getting my puck to keep moving when it hits the side. I'm using
function Update () {
transform.position.x = Mathf.Clamp(transform.position.x,-26.55, 26.5);
transform.position.z = Mathf.Clamp(transform.position.z,-53.9, 53.97);
}
to detect when the puck hits the side of the table but how do I get it to bounce back?
Comment
Answer by cowlinator · Mar 25, 2012 at 02:44 AM
The easiest way would be to let the unity engine handle it instead of trying to code it yourself. Just attach a rigid body to the puck, and put colliders on the sides.
If you want to code it, you'd have to calculate the speed and direction of the puck, take the difference of the puck movement to the 'normal' of the side of the table, stop the puck, rotate 2x that angle around Vector3.up, and send it off with the same velocity.
Your answer
![](https://koobas.hobune.stream/wayback/20220613065428im_/https://answers.unity.com/themes/thub/images/avi.jpg)