• 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 cheese_flavour · Mar 26, 2016 at 09:06 AM · 2dcollisiontrigger

How do I get collision and trigger to work?

I'm new to Unity and am trying to create a simple 2d game. However I cannot get collisions or triggers to work under any circumstances. I even created an entirely new 2d project in unity with only 2 objects colliding to remove any other factors but was unable to get it to work. Here is the C# script I've been using:

 using UnityEngine;
 using System.Collections;
 
 public class Test : MonoBehaviour {
     void Start()
     {
         print("Start: " + gameObject.name);
     }
 
     void OnTriggerEnter(Collider other)
     {
         print("OnTriggerEnter: " + gameObject.name);
     }
 
     void OnCollisionEnter(Collision col)
     {
         print("OnCollisionEnter: " + gameObject.name);
     }
 }

Because this is a completely fresh project the collision matrix (for both 3d and 2d) are set to interact with all layers, so that's not the issue. I also make sure to have colliders on objects and a rigidbody on one of them.

Based on search results for similar problems I've tried the following:

I just create 2 game objects, 1 circle to drop on a square. For the square I just add a default 2d box collider and the script. For the circle to be dropped I added a default 2d rigidbody and a default 2d box collider. Gravity will do the rest.

  1. When I start the game I only get the start message from the script. The circle drops and stops at the square, but no further messages are fired. I expected the OnCollisionEnter message to be fired.

  2. If I set isTrigger on the square, the circle drops through it as expected, but no message is triggered. Similar behaviour if i set isTrigger on the circle or both, however since the circle has no script I wouldn't expect that to work.

  3. I tried switching to continuous collision detection on the rigidbody, but this did not help.

  4. I tried increasing the mass on the rigidbody, using several different values up to 10000, however this had no effect (nor should it?).

  5. I tried moving the script to the circle, and also having the script on both. The only message which is ever fired is still the start message for each object.

  6. I tried using 2d circle colliders, edge colliders and polygon colliders.

In my actual project I've tried moving the objects with rigidbody.AddForce and rigidbody.velocity instead of just using gravity. However this worked no better.

What could be the problem?

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
1
Best Answer

Answer by LazyElephant · Mar 26, 2016 at 09:21 AM

You're using the 3d versions of OnCollisionEnter and OnTriggerEnter but using 2d rigidbodies and colliders. You need to use OnCollisionEnter2D and OnTriggerEnter2D.

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.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Trying to get bool statement to work in OnTriggerStay2D 1 Answer

Bounds.Intersects returns false while collision still works? 1 Answer

Problems with using OnTriggerStay2d 0 Answers

2D: Destroy object with dynamic collider after exiting object with static collider 1 Answer

Detect the objects staying on top of the Particles 0 Answers

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