- Home /
Question by
fantasticmrfox · Dec 21, 2015 at 04:25 PM ·
physics2dcollision2dreflect
How does Collision2D.relativeVelocity Work?
I am trying to to get a ball to have an extra bounce off certain platforms.
I think this code should work but the issue seams to be that the relativeVelocity is not what I expect it should be. Sometimes it gives me a vector that already seems to be reflected and sometimes it doesn't. It seems to be unpredictable. Has anybody else tried using Collision2D.relativeVelocity?
var bounceConstant:float = 20;
function OnCollisionEnter2D(coll:Collision2D)
{
var vel:Vector2 = coll.relativeVelocity;
var normal:Vector2 = coll.contacts[0].normal;
Debug.DrawRay(coll.contacts[0].point,vel,Color.red);
var force:Vector2 = Vector2.Reflect(vel,normal);//-(2*Vector2.Dot(normal,vel)*normal - vel);
Debug.DrawRay(coll.contacts[0].point,force*bounceConstant);
coll.rigidbody.AddForce(vel*bounceConstant);
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612053156im_/https://answers.unity.com/themes/thub/images/avi.jpg)