How to make enemy detect a character in his 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);
}
}
}
finding distance is far better than using a trigger. just find the distance between the player and enemy. like
Transform _player;
void Start(){
_player = GameObject.findWihtTag("$$anonymous$$ainCharacter").transform;
}
void Update(){
if(Vectro3.distance(transform.position,_player.position) < inRange){
// do something
}
}
Answer by AntGoober · Aug 20, 2019 at 11:19 AM
Add a collider (sphere) to your enemy to act as your detection range and set as trigger.
void OnTriggerStay(Collider other) { if (other.tag == "Player") { FollowPlayer(); } else { StandStill(); } }
I tried as what you said it seems like it doesnt work heres the code i made:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyRange : $$anonymous$$onoBehaviour
{
public float speed;
public Transform player;
private Animator animator;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
}
private void OnTriggerStay(Collider other)
{
if (other.tag == "$$anonymous$$ainCharacter")
{
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, player.position, speed
* Time.deltaTime);
}
else
{
animator.SetBool("isIdle", true);
}
}
}
try print (other.name) to see if player is triggering the collider. Also, make sure the tag is exactly "$$anonymous$$ainCharacter", no space etc. Use a circle collider if it's 2D I guess, make sure you tick is trigger.
Still cant get it work tried using collider but it doesnt even prints out what i have on code. What am I doing wrong?
private void OnTriggerStay(Collider other)
{
if (other.tag == "Player")
{
Debug.Log("Is in Range" + other.name);
{
transform.position = Vector2.$$anonymous$$oveTowards(transform.position,
player.position, speed * Time.deltaTime);
}
}
else
{
animator.SetBool("isIdle", true);
}
}
Try using the check distance method as mentioned above then...
public Transform playerT;
void Start(){
playerT = GameObject.FindWithTag("Player").transform; // (Check in inspector to see that it is finding the player. If not you probably have something wrong with your tags. If so, assign the player in the inspector by dragging him into the relevant field.)
}
void Update(){
if ((Vector3.distance(transform.position, playerT.position) < chaseRange) { ChasePlayer(); } else { StandStill(); } }