Hi there,
I have a problem with the OnCollisionEnter method. The situation is this. I have a moving truck with a rigidbody and a container with 6 box colliders attached as a child. Like you can see in the screen my truck is doing random moves on this street like braking, changing lanes, rumbling over the sidewalk and so on. Everything is done with physics so the objects inside (also rigidbodys) fly around. This works fine so far.
My problem is that I want to calculate the damage which the objects have taken during the ride. So far I use coll.relativeVelocity.magnitude
for that. But this is used best in OnCollisionEnter in a script attached to the objects. This event is immediately fired by the floor so the collisions with the walls are ignored, because it is a compound collider (Except it leaves the floor, but that happens rarely). I also tried it in OnCollisionStay but then the friction with the colliders cause the most damage. Sadly I can’t make use of the collision.contacts
because they haven’t something like relativeVelocity.
If I attach rigidbodys to the box colliders the truck behaves totally different and can’t even move forward.
I think my best approach was to create a CollisionListener script and attach it to the 6 box colliders. But somehow the OnCollisionEnter isn’t even fired.
public class CollisionListener : MonoBehaviour
{
void OnCollisionEnter(Collision coll)
{
Debug.Log("Here");
if (coll.gameObject.tag != "Finish")
{
if (coll.relativeVelocity.magnitude > 0.9f)
{
coll.gameObject.GetComponent<Damage>().GiveRelativeVelocity(coll.relativeVelocity.magnitude);
}
}
}
}
So is there any solution how I can handle more collisions in the OnCollisionEnter, access the single box colliders, calculate the impact velocity in a different way or something I have tried so far in a correct way :-).