- Home /
Negating a function.
I want to simply negate this function, so it only works if it isn't colliding with anything.
var Thing : GameObject;
function OnTriggerStay (other : Collider) {
if (Input.GetButtonDown("Enter")) {
Instantiate(Thing, transform.position, transform.rotation);
}
}
Answer by GesterX · May 08, 2011 at 10:10 AM
You should use OnTriggerExit for this (obviously make sure your collider has isTrigger set to true). Other wise you can use OnCollisionExit. I would also advise not using OnTriggerStay as it is called every frame and could be a performance killer.
What you should do is have a boolean which get's set to true/false when the trigger area is exited and then check that boolean in your update method. Something like this:
var inTrigger;
function OnTriggerEnter() { inTrigger = true; }
function OnTriggerExit() { inTrigger = false; }
function Update() { if (!inTrigger) //if not in trigger { //do stuff } }
The same could be achieved by setting the boolean to true in OnTriggerStay() but I prefer to do it as above. Either way will work
Answer by Cyb3rManiak · May 08, 2011 at 12:58 PM
You can also use Physics.CheckSphere, Physics.OverlapSphere, Physics.CheckCapsule and other methods from the Physics class to do this yourself.
I'm posting these as additional options, but I think that GesterX's way is the right one unless you really need something unique and you know what you're doing.
Your answer
Follow this Question
Related Questions
Error with function OnTriggerXXX 1 Answer
OnCollision and OnTrigger being called twice 3 Answers
Change scene with trigger collision not working. 1 Answer
Question about collision and trigger 2 Answers