- Home /
How to activate / deactivate an enemy on player proximity
I have no idea how to activate / deactivate enemies when the players comes close / moves out of range (so they don't cost cpu cicles). The only thing I can think if is calculation the position from the enemy to the player ondate, but if you put 100 enemies in the game that would mean 100 distance calculation per update. There must be a more effective way to do this, because almost all games would need something like this to keep it fast.
So is there a way to totaly disable an object? And how do i do this using player proximity.
Answer by Ashkan_gc · Jun 22, 2011 at 01:10 PM
you can use a trigger around your enemy. attach a trigger (a big one) to a gameObject you make it the children of your enemy. then attach a code like this to it.
function OnTriggerEnter (other : GameObject)
{
if(other.tag == "Player") transform.parent.enabled = true;
}
function onTriggerExit(other : GameObject)
{
if(other.tag == "Player") transform.parent.enabled = false;
]
the code assumes that the player has a player tag assigned to it but you can identify the player in any other way. the transform.parent.enabled also enables/disables the parent of the object but you can declare a public variable and attach the enemy to it in the editor if you want. also you might want to enable/disable a script of the enemy so you can declare the variable of type "thatScript". where thatScript is the name of your script.
var enemy : EnemyAI;
and then enable disable the enemyAI script of the object attached to this variable in editor or using GameObject.Find and GetComponent.
in this way using triggers you will not call the code in update and they are really cheap even on ios triggers don't take much. also you can make big triggers for each reagon to do enable/disable on a per reagon basis.
Why do you do "transform.parent.enabled = false", isn't disabling the scripts enough? What does it do?
Answer by Eric5h5 · Jun 22, 2011 at 09:45 AM
You wouldn't have to do it per update; you could use coroutines or InvokeRepeating to do it 10 times per second or something. It's probably better if you use large sphere triggers for the enemies, though. Then you can do
function OnTriggerEnter () {
enabled = true;
}
function OnTriggerExit () {
enabled = false;
}
Answer by projectInxomplete · Jun 22, 2011 at 07:41 PM
Thanks, I've taken the trigger aproach, I've created a script which I attached directly to the enemy. My final (and working) code:
private var AIScript:EnemyAI;
function Awake() {
var sc : SphereCollider;
sc = this.gameObject.AddComponent ("SphereCollider");
sc.radius = 50;
sc.isTrigger = true;
AIScript = GetComponent(EnemyAI);
AIScript.enabled = false;
}
function OnTriggerEnter(other:Collider){
if(other.tag == "Player") AIScript.enabled = true;
}
function OnTriggerExit(other:Collider){
if(other.tag == "Player") AIScript.enabled = false;
}
Answer by ahsen35813 · Apr 02, 2021 at 11:17 PM
You could use a script like this:
float activationDistance = 5f;
GameObject enemy;
GameObject player;
void Update(){
if ((enemy.transform.position - player.transform.position).magnitude < activationDistance)
{
enemy.enabled = true;
}
else
{
enemy.enabled = false;
}
}
Put this script on a parent object with the enemy as the child object because a disabled gameObject can't re-enable itself. Sorry for the bad code formatting, but it should work. Pretty reliable and should work even if the enemy travels too fast to cause problems with colliders and things, since it's based on just the enemy's distance from the player. In my opinion, this is a more reliable system and a bit simpler too.
Your answer
Follow this Question
Related Questions
enemy waypoints and detection scipting 2 Answers
Is there any command that can reference deactivated objects? 1 Answer
Expecting : found ; error 1 Answer
Enemy follows player then attacks! 2 Answers