• 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 BlueKickshaw · Mar 07, 2015 at 05:18 PM · c#2dontriggerenterontriggerexit

OnTriggerEnter/OnTriggerExit *timing* not consistent?

So, here's the scoop. My character is in a corner, and it wants to check whether or not it can change its position by 1 unit. It sends a 'target' location object, which will say "hey I'm in a trigger!", grab the tag of the trigger, then see if it can move. using UnityEngine; using System.Collections;

 public class PlayerMovementCheck : MonoBehaviour {
     public bool isFree;
 
     void Start(){
         isFree = true;
     }
 
     void OnTriggerEnter2D(Collider2D other) {
         if (other.tag == "_solidObject") {
             print ("Collision detected");
             isFree = false;
         }
     }
 
 
 
 
     void OnTriggerExit2D(Collider2D other) {
         print ("Collision no longer present");
         isFree = true;
     }
 }

It works flawlessly, with some flaws. Funny how coding works like that. If I'm going head on in to a wall, it works. If I go in to a corner and hit a wall, it works. HOWEVER. Let's say I'm in the bottom right corner of a square (top down, 2D). I go down. Wall detected, canMove = false;. I go left now. It leaves, canMove = true; but WAIT! Another wall! canMove = false;. Cool!

But let's say I go left first. canMove = false; since there's a wall. But then I go down. Now it detects the wall and canMove = false;. But it just left a trigger? So clearly we should set canMove=true;. End result? I just pulled a Patrick Swayze in Ghost and walked through a wall. Just by changing the order of the keypress, I can walk through walls. assuming I'm in a corner. In certain circumstances, I can even walk through walls by quickly changing the direction of the targetted location.

http://i.imgur.com/Xx8AhNw.png (Example of collision printout).

So uh... any idea how I can fix this? Can I make my OnTriggerExit/Enter more consistent? Are there any recommended alternatives?

Comment
Add comment · Show 2
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 VipHaLongPro · Mar 07, 2015 at 05:24 PM 0
Share

looks like you enable IsTrigger, so why not disable it (as by default) and handle the OnCollisionEnter2D / OnCollisionExit2D ins$$anonymous$$d? Then you don't have to manually stop the object from moving through the wall.

avatar image Glurth · Mar 07, 2015 at 05:39 PM 1
Share

It sends a 'target' location object, which will say "hey I'm in a trigger!", grab the tag of the trigger, then see if it can move.

Woah, that's alot of work! Why not just use a raycast to see if there is a collider in that direction?

(but I prefer VipHaLongPro's suggestion to just let unity handle not passing though other objects. heck you can even make 'em bounce.)

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Pendantic · Mar 10, 2015 at 09:40 AM

I would definitely look into raycasts, think of them as lasers that tell you if they hit something. A possible reason why your specific code isn' working could be because your OnTriggerExit2D will go no matter what you exit and and since it is possible for an exit of another cube to fire while you are simultaneously in a another cube then it could turn to true. Although I would recommend raycasts if you want to make this work try changing on triggerenter into OnTriggerStay.

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 BlueKickshaw · Mar 09, 2015 at 09:17 PM

Ran in to some other issues with colliders, but raycasts work beautifully. I don't know why I didn't think about it, cheers!

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 Glurth · Mar 09, 2015 at 11:00 PM 0
Share

I know how that goes, once you get rolling on a particular train of thought, it can get hard to change tracks.

Glad raycasts are working for you :)

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

OnTriggerEnter works, OnTriggerExit doesn't 3 Answers

OnTriggerExit doesn't fire when changing RigidBody (bug? feature?) 2 Answers

Audio not playing after OnTriggerStay is applied to the scripts (Driving Simulator Project) 0 Answers

Trigger on MeshCollider Only Seems To Work at Edges of Mesh 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