Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SavvaMadar · Oct 17, 2012 at 12:29 AM · parentchild

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SavvaMadar · Oct 17, 2012 at 02:15 AM 0
Share

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!

avatar image dannyskim · Oct 17, 2012 at 02:31 AM 0
Share

Sure thing. Hope that does it for you.

avatar image SavvaMadar · Oct 17, 2012 at 03:56 AM 0
Share

Sudden realization here, I use raycast not an actual bullet how would I check collision detection? Or maybe I'm just being stupid...

avatar image SavvaMadar · Oct 17, 2012 at 03:58 AM 0
Share

I know how to check collision detection using tags but ID$$anonymous$$ how to do actual colliders with raycast.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Changing a childs Orientation with iTween 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges