- Home /
how to force a trigger to detect enemy not moving
hi is this a way to force a trigger to detect an object in x time?? something like a predefined function in unity.
Do you mean ins$$anonymous$$d of picking up the ammo can when you get near, you have to stay near for 2 seconds? There's nothing special for that -- just program$$anonymous$$g to manually count-down and make sure you're still near it.
Answer by aldonaletto · Mar 17, 2012 at 12:25 AM
This question isn't clear. There are trigger events to detect when an object enters the trigger (OnTriggerEnter), exits the trigger (OnTriggerExit) or to be fired while the object is inside the trigger (OnTriggerStay - sent each physics cycle).
If the trigger is static, it will detect moving rigidbodies or CharacterControllers that enter it. If the trigger moves and the object is static, add a kinematic rigidbody to the trigger.
Answer by rafrafi · Mar 18, 2012 at 01:05 AM
i want trigger detect a no-moving character controller so i want a function that force detecting in the time i want.
Answer by Kleptomaniac · Mar 18, 2012 at 03:42 AM
Hmm, still not very clear but perhaps something like this:
private var wasStoppedSinceNow : float; //Typecast as a float so you can increase by decimal increments if you want
private var timeToStop : float = 5;
private var rate : float = 1;
function OnTriggerStay (hit : Collider) {
var targetHit : GameObject = hit.collider.gameObject;
if (targetHit.tag == "enemy" && wasStoppedSinceNow >= timeToStop) {
if (targetHit.GetComponent("CharacterMotor").movement.velocity == Vector3.zero) {
//Do whatever you want to do here and then once your function is finished
wasStoppedSinceNow = 0;
}
} else if (wasStoppedSinceNow < timeToStop) {
wasStoppedSinceNow += Time.deltaTime * rate;
}
}
I'm not even sure if this will work, let alone if this is what you want.
EDIT:
Changed the script to account for time ... haven't tested so it won't be perfect ... I'd say your question is probably too generalised and unspecific to generate a perfect response anyway, but whatever.
Hope that helps, Klep
For the part about time, you add an extra wasStoppedSinceNow
variable. Enter sets to the current time (or 9999 -- something that says "no".) Likewise the "else" would also do that. Your if part resets to current time (if it was 99999) and "fires" if wasStoppedSinceow
was long enough ago.
Checking veloicity == 0 is safe for a standard CarController script. Unity will quickly set speed to exactly 0. For a rigidbody script, I'm not sure that speed would even be 0 (for example, gravity may keep y at -0.001.)
Thanks Owen, completely forgot he wanted to account for time. I edited my script, however I'd say there are some areas that may need review ... :P
Oh, and in this case he said he was using a CharController so I didn't account for rigidbodies ...