Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 rjs_chipmunk · Dec 10, 2016 at 01:30 PM · c#2dcollision detection

I cant get 2D collisions to work, but everything looks ok to me, any help please?

I am very new to unity and programming genrally but im trying to make a frogger like game, i cant get my player to hit any enemies, heres my script...

using UnityEngine; using System.Collections;

public class DeathTrigger : MonoBehaviour {

 void OnCollisionEnter (Collision col){
     
     if (col.gameObject.tag == "Enemy")
     {

         print ("Hit!");
     }
 }

}

From all the online stuff ive read this should work, but im getting no joy, ive got two objects, both with box colliders 2D and both have rigidbody 2D's. Im probably missing something super simple but all the same...

This has been bugging me for a while and any help is more than welcome, thanks.

P.S. Using Unity Ver: 5.4.1 and writing in Csharp.

Comment
Add comment · Show 1
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 rjs_chipmunk · Dec 10, 2016 at 05:04 PM 0
Share

I have also recently tried setting the collisions to look for the name of the object being collided with rather than the tag to no avail...

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by UnityCoach · Dec 10, 2016 at 07:11 PM

If I remember well, the documentation is misleading, or the Collision2D model is working like expected.

The .gameObject property will return the gameObject the component is on. Try using .collider.gameObject instead.

 void OnCollisionEnter (Collision col)
 {
      if (col.collider.gameObject.tag == "Enemy")
      {
          print ("Hit!");
      }
  }
Comment
Add comment · 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
0

Answer by rjs_chipmunk · Dec 10, 2016 at 10:43 PM

Thanks for the reply but it still doesnt work, like i said i am new to this, so are there any inspector settings i might have wrong?

The 2 screenshots are of the 'truck' or the enemy, and of the player.

Thanks again.


screen-shot-2016-12-10-at-224032.png (185.6 kB)
screen-shot-2016-12-10-at-224006.png (177.5 kB)
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 UnityCoach · Dec 11, 2016 at 07:59 AM 0
Share

For a collision to happen, none of the two must be set to trigger. Otherwise, they OnCollisionEnter will never be received, and you should use OnTriggerEnter.

Also, I just realise you work in 2D. You must use OnCollisionEnter2D ().

avatar image
0

Answer by kynian · Dec 11, 2016 at 08:57 AM

One is set as a rigidbody collider, the other is set as a static trigger collider. This means you would need to handle triggers not colliders. So you would want either OnTriggerEnter or OnTriggerEnter2D. Look at this link for a chart on which setups will cause collisions vs triggers: https://docs.unity3d.com/Manual/CollidersOverview.html

Comment
Add comment · Show 3 · 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 rjs_chipmunk · Dec 11, 2016 at 10:16 AM 0
Share

i changed the triggers and now when i hit the truck my player sort of bounces around him, so the colliders are recognising eachother but its still not doing anything beyond that...

thanks for the reply.

avatar image rjs_chipmunk · Dec 11, 2016 at 10:29 AM 0
Share

i get this console error when i change the 'OnCollisionEnter' to an 'OnTriggerEnter' (it wouldnt let me upload an image, but the error is this... "Script Error:OnTriggerEnter This message parameter has to be of type:Collider") It runs fine in the script editor, i just get the error 'in game' as it were.

avatar image kynian · Dec 11, 2016 at 12:10 PM 0
Share

That's right. The error your giving gives you a hint. In this case ins$$anonymous$$d of passing in a collision you're passing in a collider. So it should be something like OnTriggerEnter(Collider collider).

See this documentation for more information: https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html

Also note if you're working in 2d there is also a method called OnTriggerEnter2D(Collider2D collider) that you can use as well.

avatar image
0

Answer by brunocoimbra · Dec 11, 2016 at 12:43 PM

You are using OnCollisionEnter(Collision col), use OnCollisionEnter2D(Collision2D col) instead.

Comment
Add comment · 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

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

296 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 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 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 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 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

Issues with 2D collision/overlap detection,Help detecting 2D collision/overlap 0 Answers

Issue with 2D collision/overlap detection 0 Answers

Projectile colliding with player at origin,Excluding a specific game object 0 Answers

OnCollisionEnter2D not calling, but objects are colliding. 1 Answer

Detect whether a Rigidbody collided with a Trigger 2 Answers


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