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 KingCrizzo · Oct 21, 2012 at 05:05 AM · collisiononcollisionentertags

Why is my OnCollisionEnter function working for enemies and not my player?

I am trying to use tags to limit the OnCollisionEnter function to only trigger when a certain type of shot collides. I have nearly identical functions on my enemies as I do on the player, yet the one on the player is not working.

On the enemy I put:

 function OnCollisionEnter(collision : Collision) {
     //Player shot hits enemy, enemy explodes, score points
     if(collision.gameObject.tag == "PlayerShot"){
         gameController.AddScore (scoreValue);
         Instantiate(explosion, transform.position, transform.rotation);
         Destroy (gameObject);
         }
     //Player collides with enemy, both die, game over
     if (collision.gameObject.tag == "Player"){
         Destroy (collision.gameObject);
         Instantiate(explosion, transform.position, transform.rotation);
         gameController.GameOver();
         Destroy (gameObject);
         }
 }

Which is working as intended. Enemies do not blow themselves up with their own shots and die when shot by the player.

On the player I have:

 function OnCollisionEnter(collision : Collision) {
     //Enemy shot hits player, player explodes, game over
     if(collision.gameObject.tag == "EnemyShot"){
         Instantiate(explosion, transform.position, transform.rotation);
         gameController.GameOver();
         Destroy (gameObject);
     }
 }

Everything is correctly tagged and I'm not getting any errors, but the enemy shots just pass right through. What am I missing?

Comment
Add comment · Show 5
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 Fattie · Oct 21, 2012 at 05:35 AM 0
Share

you likely need to get in to unity's very awesome LAYERs system. physics layers. check out the doco

avatar image tavoevoe · Oct 21, 2012 at 07:26 AM 0
Share

If you have a character controller on your player, they don't always get OnCollisionEnter called on themselves. You could try: http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.OnControllerColliderHit.html ins$$anonymous$$d.

avatar image KingCrizzo · Oct 23, 2012 at 07:30 PM 0
Share

Neither of those suggestions work. They are already on the same layer, so until I solve the detection issue putting them on a different layer does nothing.

Also I wrote my own code to control the player. It does not have a character controller on it.

Edit: Put a rigidbody on the enemy shot and it worked.

avatar image kconwaykyle · Oct 23, 2012 at 08:29 PM 0
Share

is your player tagged as player?

avatar image spiceboy9994 · Oct 23, 2012 at 09:16 PM 0
Share

Does your player have a rigidbody and a Collider attached?, As far as I know, OnTriggerEnter requires a rigidbody if you're not using CharacterControllers. Let me know if that worked for you

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Xroft666 · Oct 23, 2012 at 09:16 PM

Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached.

http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html

Comment
Add comment · Show 1 · 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 KingCrizzo · Oct 23, 2012 at 09:48 PM 0
Share

That fixed it. Put a rigidbody on my player and it worked. Thanks!

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

13 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 avatar image avatar image

Related Questions

OnCollisionEneter() does nothing C sharp 0 Answers

Boo & Collision 1 Answer

Objects in my scene are not responding to OnCollisionEnter 0 Answers

Using collision with Vector3.MoveTowards 3 Answers

Unity3D Playing Two Sounds - OnCollision 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