Sorry I know I just posted this but I found the answer, for those looking for the same solution I needed to add a collider to my main character instead of just using the character controller… that was my mistake.
Hi all, I have been racking my brain for the last couple days trying to figure this out on my own but even after looking at example scenes and copying what their doing I still can;t get the OnTriggerExit function to work… here is the setup.
I have a bunch of prefab objects. I want it so when my character walks up to one of the objects, that object’s OnTriggerEnter turns on and when the character walks away from it it turns off.
I have the OnTriggerEnter working and firing but the OnTriggerExit does nothing and does not fire.
The objects have a sphere collider with “is trigger” checked and they have a rigid body attached as well with gravity turned off and iskinematic turned on, collision is discrete.
The character has a character controller attached as welll as several scripts and a rigid body as well with gravity unchecked and iskinematic unchecked and collision is set to discrete.
Here is the code for the static objects or the piece that is relevant anyway.
function OnTriggerEnter(collider:Collider){
var playerStatus : CharacterStatus = collider.GetComponent(CharacterStatus);
if(playerStatus == null) return;
if(pickedUp) return;
Debug.Log ("Looking at object");
ShowRing();
//everything's good, so put it in the inventory
var characterInventory = collider.GetComponent(CharacterInventory);
characterInventory.GetItem(itemType, itemAmount);
pickedUp = true;
//Destroy(gameObject);
}
function OnTriggerExit(collider:Collider){
//var playerStatus : CharacterStatus = collider.GetComponent(CharacterStatus);
//if(playerStatus == null) return;
Debug.Log ("object untouched");
HideRing();
}
what you are saying is that I must remove my Character Controller and use a simple Capsule Collider?
So, there is no problem with the gravity and the movement? I’m calculating the gravity by my own like in the example of the ThirdPerson Controller and I’m using a the path that returns a PathFinding to get move. So, this didn’t bring you more problems than the CharacterController??
If you are using two collider on the same game object ontriggerexit will never be called. For example if you have on the same game object a character controller and a sphere collider with trigger on ontriggerexit will never be called because it is colliding with the character controller. You can detect collision using ontriggerstay otherwise
A good way to do this is to put two private boolean global variable one called stay and the other called iscolliding, than into the OnTriggerStay function put this code:-
function OnTriggerStay(col : Collider){
if(col.gameObject.name==“The name of the gameobjet i want to know if i collide”){
stay=true;
}
// so if i’m colliding with myself it will not be reported
}
And into Update function put this code :-
if (stay){
iscolliding=true;
stay=false;
} else iscolliding=false;
I´m on the same problem. I don´t understand what the original post meant by the following:
“I needed to add a collider to my main character instead of just using the character controller”
Both my characters have colliders, and I still dont get on trigger exit to work