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?

`

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

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.