- Home /
Getting triggers to stop triggering on some objects once certain criteria is met
Hi. Apologies for the weird question title, found it really hard to sum up in a sentence.
I've a game at the moment where you need to place books onto a shelf, upright. This is done with triggers; when you drag a book into the trigger area and drop it, if it stays upright it's marked as correctly placed and the trigger material turns green. That is all done in OnTriggerStay. And then in OnTriggerExit, the material colour goes back to normal once the book is moved out of the area, and it's marked as nothing being placed there.
The above is working. But when a book is correctly placed, and then I try and drag another book into the trigger area, and then out again, it's marked as nothing being correctly placed there, despite the original book still being placed there — I have to click the correctly placed book for it to marked as placed. I've tried a check by saving the gameobject that's correctly placed, and then making OnTriggerExit only fire when that book exits, but I can't get it to work. (this isn't in the code below, but the gameobject being saved is 'book', which still is)
Has anyone any ideas?
// EDIT I've since recoded some parts, so putting them below. But the issue seems to be something to do with how close the object are to each other; see my comment below.
void OnTriggerEnter(Collider col){
if (col.gameObject.tag == "Draggable"){
//print ("Enter col: " + col.gameObject);
objectAttemptingEntry = col.gameObject;
}
if (bookPlaced && objectAttemptingEntry != objectPlaced) {
ignore = true;
}
else {
ignore = false;
}
}
void OnTriggerStay (Collider col) {
if (col.gameObject.tag == "Draggable"){
if (!ignore){
//print ("staying col: " + col.gameObject);
objectAttemptingStay = col.gameObject;
if (objectPlaced == null || objectPlaced == objectAttemptingStay){
if (objectPlaced == null){
print ("Object Placed is Null. " + objectAttemptingStay + " is attempting Stay");
}
if (objectPlaced == objectAttemptingStay){
print ("Object Placed is objectAttemptingStay. " + objectAttemptingStay + " is attempting Stay");
}
GameObject temp = objectAttemptingStay;
DraggableItem item;
item = temp.GetComponent<DraggableItem>();
// check the orientation of the book type to see if it's close enough to be correct, depending on the orientation specfied above
if ( (CloseEnough(temp.transform.rotation.x, orientationF_up_A, 0.5f))
|| (CloseEnough(temp.transform.rotation.x, orientationF_up_B, 0.5f))
|| (CloseEnough(temp.transform.rotation.x, orientationF_down, 0.5f)) ){
// correct book, correct orientation
gameObject.renderer.material.color = Color.green;
bookPlaced = true;
item.inPlace = true;
objectPlaced = temp;
}
else {
// correct book, wrong orientation
gameObject.renderer.material.color = Color.red;
bookPlaced = false;
item.inPlace = false;
}
}
else {
objectAttemptingEntry = null;
//objectAttemptingStay = null;
}
}
}
}
void OnTriggerExit (Collider col) {
if (col.gameObject.tag == "Draggable") {
//print ("Exit col: " + col.gameObject);
objectAttemptingExit = col.gameObject;
if (objectAttemptingExit != objectPlaced){
print ("objectAttemptingExit is not the placed object. Returning");
objectAttemptingEntry = null;
objectAttemptingExit = null;
return;
}
else {
if (objectPlaced){
print ("objectAttemptingExit is objectPlaced. Exiting.");
DraggableItem item;
item = col.gameObject.GetComponent<DraggableItem>();
gameObject.renderer.material.color = Color.white;
bookPlaced = false;
item.inPlace = false;
objectPlaced = null;
objectAttemptingExit = null;
objectAttemptingStay = null;
}
}
}
}
cant you just place a bool statement around it all so if its done it sets it to false, not allowing them to enter the code anymore?
That doesn't seem to work. Everytime I put in checks like that, it seems to ignore them, or the OnTriggerExit resets them when I don't want them to.
I'm trying to get it so if the col.gameobject that is exiting isn't the original gameobject that's there,(the one that's saving to 'book') then it simply returns out of OnTriggerExit, but it's ignoring that check for some reason.
Likewise, I'm trying to get it so the OnTriggerStay function code only fires when the book object is null. But that also seems to get ignored.
I've narrowed it down that it's something to do with distance between the objects, or something like that.
I've added in loads of checks to see what's exactly going on, and still couldn't figure it out. But then I made the trigger area much bigger, and have now noticed that it's something to do with how close the objects are. If I move a new object into a trigger area that has a book placed correctly in it, everything works at first; I can drag in a bit, drag it out a bit, and everything works as it should. But if I move it to within a certain distance of the placed book object, then the triggers seem to think that this new object is now the placed object, so everything breaks down.
It doesn't seem to be just distance though. Sometimes if I drag in from the left, I can get the objects pretty close together before it goes wrong, other times I bring it from the top or elsewhere and it breaks almost immediately. I've checked the colliders of the two objects as well; it can break whether they overlap with each other or not.
I'm finding this problem to be really frustrating as it doesn't seem to make any sense.
I've edited my question above with the new code I've done since. Though now I'm not so sure if it's even a coding problem.
Answer by PotatoInsertion · Jul 24, 2014 at 10:29 AM
I eventually scrapped all this code and redid it with a spherecast. Now instead of a trigger checking for books, a spherecast comes out of the trigger area and does the job.
As I couldn't get this to work with Triggers in the end, this doesn't really answer my question, so I won't mark this answer as correct. If someone could figure out an answer the actual question, I'll happily mark it.
From what I could figure out, it's something to do with proximity of the two objects. When I moved an object close (how close or not doesn't seem to be a constant figure) to a correctly placed object (whether their colliders overlapped or not didn't seem to matter), then the correctly placed object's OnTriggerExit function would call; despite there not being any movement to that object. This happened whether the object being moved (ie, not the one correctly placed inside the trigger) entered the trigger area or not.
Makes no sense to me anyway. Maybe it's a Unity bug, or maybe I was just missing something.
Your answer