- Home /
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?
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.
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.)
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.
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!
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
Follow this Question
Related Questions
OnTriggerEnter/Exit? 1 Answer
OnTriggerEnter/Exit not called sometimes 0 Answers
NavMeshAgent and OnTriggerEnter/OnTriggerExit 0 Answers
Have "OnTriggerStay" detection but only call it once? 1 Answer