• 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. Make 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

How to check if a collider is Touching any other object 2 Answers

Jump off 2D platforms Smash brothers style. 1 Answer

Collisions not working in top-down RPG? 0 Answers

C# - My increament code is not working? 1 Answer

2D Collisions Not Working,2d Collision not working? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges