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 aristaios · Jan 03, 2014 at 06:56 PM · 2dcolliderspritetrigger

Why doesn't OnTriggerEnter2D get called?

I just started using the 2D tools in Unity but this behavior seems a little odd: I have two sprites with (for testing purposes) each a Box Collider 2D on them, both set to isTrigger.

One has a script attached to control it and register collisions, but the method never gets called even though they should collide! Also if this for some (obvious) reason can't work, what would be a work around without using Rigidbody2D?

PlayerController.cs:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {

     void Start () {
     
     }

     public void Update()
     {
         float horizontal = Input.GetAxis("Horizontal") * 0.1f;
         float vertical = Input.GetAxis("Vertical") * 0.1f;
 
         transform.Translate (horizontal, vertical, 0);
     }
 
     void OnTriggerEnter2D(Collider2D other) {
         Debug.Log ("was hit");
     }    
 }




EDIT

Noticed the thing about triggers and reverted to normal BoxColliders. Both Objects are on the same z-value. To clear things up:

PlayerObject:

  • SpriteRenderer

  • PlayerControllerScript (see below)

  • BoxCollider2D

OtherObject:

  • SpriteRenderer

  • BoxCollider2D

    void OnCollisionEnter2D(Collision2D coll) { Debug.Log ("was hit"); }

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
0

Answer by maxkunes · Jan 03, 2014 at 07:31 PM

 function OnCollisionEnter2D(coll: Collision2D) {
 if (coll.gameObject.tag == "Tag") {
 }
 }

That is my function to do triggers/collision and it works perfectly

Note!! Collision 2d will only collide with a object with a 2d collider

Comment
Add comment · Show 5 · 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 aristaios · Jan 03, 2014 at 10:58 PM 0
Share

Sorry, but that is as unhelpful as anything. I clearly wrote BOTH objects have a BoxCollider! I am out of the blue assu$$anonymous$$g that you are indeed using a Rigidbody2D on your object which is what I didn't want to do.

avatar image maxkunes · Jan 03, 2014 at 11:21 PM 0
Share

How is that unhelpful and by the way i am not using rigid body you have a unproper way of registering collisions and triggers.

If you did not know a collision is the same as a trigger in most conditions and also if you are using what im using the object does not need a rigidbody because in my current game right now I am using your code edited a bit of course and it is working fine with my fix maybe your missing something but I am not sure why this is not working for you but it is for me..

avatar image maxkunes · Jan 03, 2014 at 11:25 PM 0
Share

And also just curious even if this is just a test unless in the future you are going to use raycast for collisions which is a pain why would your character be a trigger because making something a trigger disables the collider itself so you would need another way of colliding with objects unless you would use a trigger collider and a seperate polygon collider.

How are you going to achieve this it will help me in future projects because i hate using raycast and triggers?

avatar image aristaios · Jan 03, 2014 at 11:32 PM 0
Share

It is unhelpful because it in no way relates to my question. Yeah I noticed the stuff about the isTrigger things and changed it.

avatar image aristaios · Jan 05, 2014 at 05:49 PM 0
Share

Bumpity bump

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

19 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

Related Questions

Unity2D: Renderer off/ on by triggers 0 Answers

Wanting to have a sprite animation to only start playing once the player collides with it? 1 Answer

why isn't my OnTriggerEnter2D() function working? 14 Answers

OnTriggerStay2D Breaks When Adding AnimationController 1 Answer

How to Address Texture2D Elements from a Sprite with Sprite Mode: Multiple, in Code? 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