- Home /
How to make enemy detect a player in a certain range?
Hello i created a CSharp code that should cover my enemy AI (enemy detects player in his range, follows him and if my character gets out of enemy detection range he stops following my character). Every works fine except that enemy follows him even outside the Enemy detection range that i set trought the my script. I need my Enemy to follow the player in certain range .
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyRange : MonoBehaviour
{
public float speed;
public float range;
public Transform player;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
if (Vector3.Distance(player.position, transform.position) <= range)
{
transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
}
}
}
Answer by omegaaicoding · Aug 23, 2019 at 12:22 AM
I can't see your other scripts or game objects that could be affecting this. All I can see in your Update method is a condition that starts the enemy following the player. There isn't anything to deactivate the following. Try adding "if (vector3.distanct(...,...) > range) {//go do something else, start patrolling waypoints, etc.}"
Hello thank you for your answer. I tried what you said but it seems like it doesnt work(maybe iam doing something wrong?)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : $$anonymous$$onoBehaviour
{
public Transform player;
private float range = 1f;
public float speed;
public Animation anim;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Vector2.Distance(transform.position, player.position) <= range)
{
transform.Translate(Vector3.$$anonymous$$oveTowards(transform.position, player.position, speed * Time.deltaTime));
}
else
{
if(Vector2.Distance(transform.position, player.position) > range)
{
anim = GetComponent<Animation>();
}
}
}
}
I don't think the structure of your code will ever run the second if-statement. Because the first if-statement has executed, the else-if will never execute.
Try removing the else and adding a debug log just to make sure the code line runs...if it does, then you will be left to whatever you want the enemy action to be.
'// Update is called once per frame void Update() { if (Vector2.Distance(transform.position, player.position) <= range) { transform.Translate(Vector3.$$anonymous$$oveTowards(transform.position, player.position, speed * Time.deltaTime));
}
if(Vector2.Distance(transform.position, player.position) > range)
{
Debug.log("Enemy is out of range and code executed.");
anim = GetComponent<Animation>();
/*create a different action later on on what you want the enemy to do, make sure you change the transform of the enemy in some way or it will still follow you AND run your animation at the same time.*/
}
}
}
} `
Hey man thanks for the answer. I forgot that i need to add the "else" statement to stop the following. So thanks.
Answer by logicandchaos · Feb 15, 2021 at 04:47 PM
You can also use OverlapCircle() to get players in range.
Answer by quimerico · Feb 15, 2021 at 03:45 PM
Awesome answer, I added the sigle line of the if statement to the stalker code I made from a tutorial, and it worked perfectly! So simple :}