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;
}
}
}
}