- Home /
OnTriggerEnter forcefield help!
I'm making game that will create a force field around an area if you turn on a certain light, I want this force field to kill all things tagged respawn that touch it. I have all the code working except that when you turn on the light, if a monster is already inside the forcefield the monster does not die unless it goes outside and then back in. I think this is because I use an OnTriggerEnter function, so it doesn't run again if the if conditions are fulfilled. It only runs when they enter. Is there a different way I could do this?
just incase it is hard to grasp my question here's my code, it works fine.`
var Monster : GameObject;
var lightObject : Light;
function OnTriggerEnter (other : Collider) {
if(lightObject.enabled == true){
if (other.CompareTag ("Respawn")) {
Destroy (other.gameObject);
Instantiate (Monster, Vector3( -60,-13,70), Quaternion.identity);
}
}
}
If your force field is a sphere you might be able to use Physics.OverlapSphere to find anything inside your force field when you turn it on.
Answer by Flickayy · Jan 27, 2013 at 07:31 PM
How about changing this:
function OnTriggerEnter (other : Collider) {
if(lightObject.enabled == true){
if (other.CompareTag ("Respawn")) {
to this:
function OnTriggerStay (other : Collider) {
if(lightObject.enabled == true && other.tag == "Respawn") {