Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Serana · Apr 29, 2016 at 11:39 AM · collisioncollision detectioncollider2dcollision2d

Collision not working

I want my player to get hit by an enemy missile, which spawns automatically. Both missile and player have rigidbodys and circular collider and are on the same layer, tags are set. This is the code I use: void OnCollisionEnter2D(Collision2D col) { if (col.gameObject.tag == "Stein") { playerhealth -= 10; Debug.Log("HIT"); Destroy(col.gameObject); } }

Even though the player get knocked back when the missile hits him, the collision is not detected. Any idea why it does not work and how I can fix it?

Comment
Add comment · Show 4
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 Scribe · Apr 29, 2016 at 12:41 PM 1
Share

Add some more debug logs, to print the col.gameObject.name and gol.gameObject.tag, before any conditional statements. $$anonymous$$ake sure they are as expected. If this code is attached to the player, add some similar code to the missile, maybe it is hitting a collider attached to the player, but not the specific one that you think it will.

avatar image Serana Scribe · May 02, 2016 at 07:17 AM 0
Share

Adding the code to the missile works, thanks! But it only works when the missile is not a trigger. But I don't want the player to get knocked back by it, what can I do to achieve this?

avatar image Scribe Serana · May 02, 2016 at 09:30 AM 0
Share

I did not necessarily mean for you to leave the code on the missile, I meant for you to add it along with some Debug statements, in order to identify what each collider is hitting, and whether it is as expected. As Toby says, if you want to use triggers, you need to change the method used from OnCollision... to OnTrigger...

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TobyKaos · May 02, 2016 at 09:24 AM

If you do not want to have force apply on Object when hit you must use OnTriggerEnter2D(Collider2D other) function instead of OnCollisionEnter2D

Be sure to have added RigidBody2D and and BoxCollider2D (or other Collider2D) on Character if you want to walk on a physic ground. Add only a BoxCollider2D( or other) on missile with the IsTrigger checked for it. No rigidbody2D on missile needed. On Missile script add OnTriggerEnter2D function.

If you want detection on character side script the add a child object on character with Collider 2D object and IsTrigger checked. In this case I think you also need a RigidBody2D on missile.

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 Serana · May 02, 2016 at 11:09 AM 0
Share

It works now, thank you for your help! :)

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

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

Related Questions

How to check if any/multiple 2D sprites neighbour/collide with eachother 0 Answers

Collision With Text 0 Answers

Physics 2D with tile collider corner problem 2 Answers

Collider Issue 0 Answers

Absolutely no collision detection in build but editor is fine 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