(fps) enemy chases player outside of range

My enemy seems to chase the player when out of range or at least that is what looks to be happening

on closer inspection, it seems to be more confusing.

with a wire gismo in use and a chase range of 8. The wire gismo does not connect with the player, however the enemy will still give chase, the inspector shows a distance of 7.7… so I’m a bit confused as to what has happened

is the distance incorrect or the gismo, or is there something else that I have missed

This also affects the distance of attacks, as enemy’s are able to attack from what appears to be 5 units of distance while the inspector shows less than 1 unit of distance.

just so anyone who sees this knows, the player in the screen shot is in the bottom right of the map

I have moved the enemy even further away from the player and distance seems to have decreased. and it seems the enemy (the one that is selected) is the only one to be provoked from frame one

You’re not showing any code, so it’s quite difficult to see where the problem lies

Hi, sorry ill paste the code now but from what I can tell, its the code shown in the videos with some added comments for my own use

Enemy AI script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class EnemyAI : MonoBehaviour
{
[SerializeField] Transform Target;//player
[SerializeField] float turnspeed = 5;
[SerializeField] float chaserange = 8f;

[SerializeField]bool isprovoked = false;

NavMeshAgent navMeshAgent;
[SerializeField] float distancetotarget = Mathf.Infinity;//set to infinate to avoid enemy chase at start


// Start is called before the first frame update
void Start()
{
    Target = FindObjectOfType<PlayerHealth>().GetComponent<Transform>();//find player objet based on playerhealth - no need to drag in inspector

    navMeshAgent = GetComponent<NavMeshAgent>();//get nav mesh for use
}

// Update is called once per frame
void Update()
{
    distancetotarget = Vector2.Distance(Target.position, transform.position);
    if(isprovoked)//check bool 
    {
        EngageTarget();

    }
    else if (distancetotarget <= chaserange)//if player is close-set bool
    {
        isprovoked = true; 
        
    }
    
    
}

public void onDamageTaken()//call via broadcast
{
    isprovoked = true;

}

void EngageTarget()
{
    faceTarget();
    if (distancetotarget >= navMeshAgent.stoppingDistance)
    {
        ChaseTarget();//follow target - close distance
    }
    if(distancetotarget < navMeshAgent.stoppingDistance)
    {
        attackTarget();
    }
}

void ChaseTarget()
{

    GetComponent<Animator>().SetBool("Attack", false);//set bool for animator
    GetComponent<Animator>().SetTrigger("Move");//set trigger animation
    navMeshAgent.SetDestination(Target.position);//follow target
    
}

void attackTarget()
{
    GetComponent<Animator>().SetBool("Attack", true);//set bool for animator
    Debug.Log("witty " + name + " " + distancetotarget);//attack target
    
}

private void OnDrawGizmosSelected()
{
    Gizmos.color = new Color(1, 1, 0, 0.75f);//show range of chase
    //Gizmos.DrawSphere(transform.position, chaserange);//useful in scene view for seeing object view
    Gizmos.DrawWireSphere(transform.position, chaserange);
}

void faceTarget()
{
    Vector3 direction = (Target.position - transform.position).normalized; //get vector between enemy and target

    Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));//set new look to be target

    transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * turnspeed); //rotate to face target with a speed

}

}

enemy attack script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyAttack : MonoBehaviour
{

PlayerHealth target;
[SerializeField] float damage = 40f;

// Start is called before the first frame update
void Start()
{
    target = FindObjectOfType<PlayerHealth>();//find player for this script in scene
}

public void AttackHitEvent()//use event flags in animaton window
{

    if(target == null)
    { return; }


    target.takeDamage(damage);//call function on player refrnced in inspector
}

}

enemy health script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyHealth : MonoBehaviour
{

[SerializeField] float hitpoints = 100;

public void takeDamage(float damage)
{
    //GetComponent<EnemyAI>().onDamageTaken();//possable way - keep here
    BroadcastMessage("onDamageTaken");//calls on gameobject or children - calls all named functions
    hitpoints -= damage;// = hitpoints - damage;
    if(hitpoints <= 0 )
    {

        Destroy(gameObject);

    }
}

}

I’m not sure if there is anything else that would affect this issue, i don’t want to full the feed with code that wont be useful so please let me know if I should show anything else

I think this is the problem. You are working with a 3D game here, but you are calculating the distance using a 2D vector. It’s completely ignoring the z-axis. You should be using Vector3.Distance(...).

1 Like

Thank you, I must have miss typed it and didn’t notice any difference when doing the earlier tutorials. fresh pair of eyes always seems to help

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms