- Home /
Error in scripting (HeadShot code)
//BodyShot
if(Hit.collider.tag == "Head")
{
var obj2 : Transform = Instantiate(BodyImpact,Hit.point,Quaternion.FromToRotation(Vector3.forward,Hit.normal)) as Transform;
obj2.transform.parent = Hit.collider.transform;
Instantiate(BloodSplash,Hit.point,Quaternion.FromToRotation(Vector3.forward,Hit.normal));
Hit.transform.GetComponent(HEALTH).HealthOfEnemy -= Damage * 100;
}
//HeadShot
if(Hit.collider.tag == "NPC")
{
var obj : Transform = Instantiate(BodyImpact,Hit.point,Quaternion.FromToRotation(Vector3.forward,Hit.normal)) as Transform;
obj.transform.parent = Hit.collider.transform;
Instantiate(BloodSplash,Hit.point,Quaternion.FromToRotation(Vector3.forward,Hit.normal));
Hit.transform.GetComponent(HEALTH).HealthOfEnemy -= Damage;
}
Ok so here is the problem. Both codes work! The problem is that the HeadShot code works but only if I tag the parent as "Head", if I tag the head as "Head" nothing happens... If I tag the head as "NPC" again nothing happens, but if I tag the parent of the head as "NPC" the normal damage is applied... What gives?
Answer by dannyskim · Oct 17, 2012 at 01:52 AM
http://forum.unity3d.com/threads/36462-onCollisionEnter-does-not-work-on-a-child-colider
Excerpt from the above link:
"The gameObject, collider, and rigidbody in the Collision parameter all refer to the topmost object involved in the collision. To determine which sub-collider was hit, you need to look at the contact points's thisCollider or otherCollider depending on whether you're calling OnCollisionEnter on the target or the projectile."
To clarify if there is any confusion, when you have a compound collider setup, which means you have child objects that have child colliders, they will all report to the Parent of all the Children in terms of collisions. Since this is the case, you need to have individual scripts on each object to determine what object was hit, and then report that back to the parent.
BTW, if HEALTH is a class that you made yourself, I would suggest you rename it to Health. In the context of C programming, something with all capital letters typically refers to a constant variable that never changes. Not that big of a deal if you're working by yourself, but if you ever plan to work with others, it's probably a good idea to get rid of that habit right now.
http://en.wikipedia.org/wiki/Naming_convention_(programming)#Letter_case_and_numerals
Hey thanks! Looks like it'll fix the problem! Also it's in JS and it's all caps cause I was being lazy at na$$anonymous$$g it. I'll tell you how it goes!
Sudden realization here, I use raycast not an actual bullet how would I check collision detection? Or maybe I'm just being stupid...
I know how to check collision detection using tags but ID$$anonymous$$ how to do actual colliders with raycast.
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
what is the problem of this code ? 2 Answers
Controlling GameObject selection across Parent/Child 1 Answer
Instantiate as child 3 Answers