Activate other objects component on trigger?

Hi there,

Now i want to have a script on an AI deactivated when outside the players view.

So my idea was to have a big box collider that covers the players view, and that would enable the components of whatever enters (OnTriggerEnter).

However i can’t seem to get this working, here’s my code from the box collider:

void OnTriggerEnter(Collider other)
    {
        EnemyAnt script = other.gameObject.GetComponent<EnemyAnt>();
        script.enabled = true;
    }

I tried it the other way around so they would deactivate when entering, and that actually works… So i dunno what the heck.

Hope someone can help :slight_smile:

use OnTriggerStay()

and say "if (my_Player_is_in) { activate_my_script } else { deactivate_my_script } // means watever except my_player would deactivate it

do u have problems in writing the script or in the idea how it should work ?

The idea i think…

Will try this one out and post later thx :slight_smile:

I’m not sure how to do the check “if (my_Player_is_in)”
Should i check for a tag or something?

And it’s not the player that should be [de]activated, but the AI around it… The player will always be inside my big “TriggerDummy”

the idea is when ur player get outside the big trigger , the AI must be disabled right ?!
here the script :

No… Maybe this will help

The center ant is the player, the big collider is my “big trigger dummy”.
The other ants are the AI, and they have an AI script attached.
Now i want an AI deactivated when it’s outside the collider, and then i want the big collider to activate it when inside.

]ok i got it , is better to do in the AI Script something that will calculate the distance between it self and the Player , just use
this condition within an update function :
Code:

var my_distance = Vector3.Distance( transform.position , GameObject.FindWithTag("Player").transform.position) ;
// Ur player must have a tag "Player"
if ( my_distance < limit_distance ) { 
  // put here the script that moves the AI body 
}

That’s actually how i got it working at this point :wink:

But there’s still alot of stuff in the AI script spending resources.
If possible.
I would like it to spend as few resources as possible outside the players view.

Say if i had 30 or even more AI’s outside the players view, it could cause some lagging. Especially beacause i also have some stuff like:
OnMouseEnter()
OnMouseExit()
That’s checked the entire time

Okay i found another work around that really is a complete other system than this…

I’m fine now i guess, thanks for your effort :slight_smile:

Cheers

Why don’t you use instead OnBecameVisible () and OnBecameInvisible () (the game object require a Mesh Renderer component)?