- Home /
Problems with OnCollisionEnter, rigidbodys and compound colliders
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 :-).
The truck should behave the same if the only things with rigidbodies are the items inside the truck.
This question could have different correct answers, and is mostly intended for discussion of best practices, you could post it in the forums ins$$anonymous$$d in order to receive more ideas.
Im using rigidbody.AddForce and rigidbody.$$anonymous$$ove to $$anonymous$$Ove the Truck, and I got pretty good results with it, so I would like to stick to this concept. How should I manipulate the truck without a rigidbody?
Truck has a rigidbody as do the items inside it - but not each part of the truck.
Yes thats what I'm doing. The Parent "Truck" got a rigidbody. In it a child "rear" with a renderer and materials. Rear got a child "Colliders" what is an empty game object. And "Colliders" got the six box colliders as children. Is this wrong?
Answer by KaRa777 · Feb 26, 2014 at 11:54 AM
I got a solution. Took me a while beacause I'm relative new to Unity. For anybody who looks it up: You have to detach the colliders and connect them with the old parent via a fixed joint. Then the walls will have an own rigidbody but will stick with the position of the truck. Now they wont act as a compound collider and you can work with the OnCollisionEnter() of each side.
Answer by MrYOLO · Feb 27, 2014 at 10:02 AM
I think Layer in unity will help you in this situation. You can set the object you wanna calculate the damage to the same layer, so the OnCollisionEnter will only be called when the things in the same layer colliding with each other. Through this, you can ignore the influence from other unecessary colliders(like floor you dont want), then in OnCollisionEnter function, you can get the damage degree you want, good luck!
Your answer
Follow this Question
Related Questions
Camera gets flung off of the map when the player collides with certain objects. 0 Answers
Ignore sub-colliders for center of mass in Rigidbody2d? 1 Answer
Physics Possibilities?: Angles, Penetration, Exceptions 4 Answers
ball's collider sometimes catches an edge and bounces. when ball rolls over two aligned platforms 1 Answer