- Home /
noob pong/bounce question
so here we go another question from someone beginning to learning programming/unity.. Sorry if this is too newbish.
This is a simple box colliding and bouncing question. Here's a "simple" code:
var boxSpeed:Vector3;
function Update(){
transform.Translate(boxSpeed*Time.deltaTime,Space.World);
}
function OnTriggerEnter(other:Collider){
boxSpeed=Vector3.Reflect(-boxSpeed,other.transform.forward);
}
With this code I can have a box move and "collide" and bounce back. But my question is how do I make it so that the box bounces based on the angle of the collision.. so for example, if I hit a horizontal plane on a 30 degree angle, it would bounce forward(not back) forming another 30 degree angle..... I'm basically trying to understand how something like this is coded. Thanks!
Answer by StephanK · Oct 23, 2010 at 09:36 AM
This code always uses transform.forward as the normal for the reflection. This will do what you are looking for as long as your ball hits the object on its front side. If you have for example a cube and you hit it on its right side the normal would be other.transform.right instead. So all you have to do is find out which normal to use and your code will work.
Your answer
![](https://koobas.hobune.stream/wayback/20220613080524im_/https://answers.unity.com/themes/thub/images/avi.jpg)