collision.relativeVelocity always zero
I'm having trouble getting relativeVelocity to show anything other than zero. The setup is somewhat simple:
The object with the rigidbody (the player) is standing still while the world moves around it. It has a Box collider which is bolted onto one of its child objects.
The object it is crashing with is travelling around a globe using transform.RotateAround, and also uses a Box collider (It does not have a rigidbody).
You'd think this would be a relatively simple setup, yet when they crash together, it triggers a collision like it should, but the relativeVelocity is always (0.0, 0.0, 0.0) (collision script is on the player root object).
Any ideas about what I'm doing wrong? Is this because I'm controlling the objects with transform instead of accelerating the rigidbody? Is there something else I could be using instead to find collision force if relativeVelocity is the wrong solution?
Answer by tormentoarmagedoom · Jun 26, 2018 at 08:42 AM
Good day.
Rigidbody component is the Physics compoenent, so relativeVelocity is between 2 objects will give you results if both objects have rigidbody.
If the player i colliding with a inmobile objecto, you only need to see the velocity of the polayer (the other is 0) .
But if you need to know it from 2 moving objects, you need to add Rigidbody to both objects.
Extension:
Mmm... I must say i never used it ,so i can not talk with experience...
Aaaaand if you get the 2 Vector3 velocity and calculate the relative via Mathematics??
Why you need toknow the relative? Is to calculate the "force" of the impact? Then do something like this:
Vector3 ImpactVector = Object1.velocity - Object2.velocity;
float ImpactForce = ImpactVector.magnitude;
So you have now a flaot number from the impact force.
Bye!
Thank you. However I am still receiving 0 in relative velocity even if both objects have rigidbodies.
Extended answer :D
(PD: Always, when you get a working answer, accept it to help other users find the good solution)
The reason the relativeVelocity would have been nice is because I want to know the direction of the impact. I would like a head on impact (in the x direction) to be immediately lethal, while a sideways impact (in the z direction) should maybe only slow the player down (Think a arcade style car touching the guard rails as opposed to crashing headfirst into a wall).
I see my issue is that neither object have a measurable velocity because I'm putting constraints on them (otherwise they just fly all over the place). Therefore I will need to find another solution. However my original question has been answered, so thank you for that.
IF you store the position of an object and store it again few moments after, you can create a vector that is the real direction of the object
The way I ended up doing it was to draw a vector from object to collision point and simply see if the direction of impact is beyond a certain value.
It's not the best solution, as it won't let me do things like variable damage depending on force of impact and such, but it's good enough for the moment.