- Home /
How to detect collision on child objects from the parent??
I am trying to write script on the parent object (an "Arm"), as it's animated, the collision volume is on its child mesh (the "Palm") and moves with the palm.
now I have the problem that I can't use OnTrigger or other collision functions from the parent...
I don't want to write a separate script for the palm... Is there a way?
Thank you guys !
Answer by Joshua · Apr 16, 2011 at 01:40 PM
You could just write a really short OnTriggerEnter () script and attach it to all children couldn't you? If you don't want to attach it to all children yourself because there are too many, do it through code. Why don't you want to write a separate script?
em just it looks cleaner to do the code in one script which defines the object... The parent object is in different states, only one of the state will use the OnTrigger function; if I am to separately script on child there needs to be an additional state check too. Just don't seem as clean .. :/
Answer by VIDesignStudios · Dec 05, 2016 at 07:44 AM
I had the same question. Sadly code is still the answer, but someone wrote a good simple code that can be placed on all children without having to customize or drag anything into it: http://answers.unity3d.com/questions/633480/how-to-detect-child-object-collisions-on-parent.html
Code:
public class ChildCollider : MonoBehaviour {
//Colliders only register a hit or collision when on the same level as rigidbody or parent. Compound Colliders need to be children. Solution is to add this code to all child colliders:
void OnCollisionEnter( Collision collision) {
this.GetComponent<Collider>().attachedRigidbody.SendMessage("OnCollisionEnter",collision);
}
}