LookAt function doesn't seem to work when searching player...

Hello, i have an enemy patrolling on AI between different stations. It does it very well (Sepromene function).

In the Update section, i tried coding a lookat so that when my enemy comes between the mindistance and the maxdistance variables, it will orient to the player and follow it instead of continuing patrolling.

Here is the code i used, i have no error, but it just doesn’t work when my player comes near the enemy, it will just continue patrolling. (i have the good tag on my Player)

thanks for your help and time

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

public class NPCrandom2e : MonoBehaviour
{
    public Transform player;
    public int MaxDist = 10;
    public int MinDist = 5;
    int tempsdedetection = 10;
    public int zonedemarche = 40;
   
    UnityEngine.AI.NavMeshAgent Agent;
    Vector3 positiondepart;
    Vector3 destination;


    void Start()
    {
        tempsdedetection = Random.Range(8, 16);
        InvokeRepeating("Sepromene", 1.0f, tempsdedetection);
      
        Agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
        positiondepart = this.transform.position;
        Agent.acceleration = 20;

    }

  void Awake()
    {
        player = GameObject.FindGameObjectWithTag("Player").transform;
    }

    void Sepromene()
    {
        destination = positiondepart + new Vector3(Random.Range((int)-zonedemarche * 0.5f - 2,
        (int)zonedemarche * 0.5f + 2), 0, Random.Range((int)-zonedemarche * 0.5f - 2, (int)zonedemarche * 0.5f + 2));

        if (Agent.pathStatus != UnityEngine.AI.NavMeshPathStatus.PathInvalid)
        {
            Agent.SetDestination(destination);
        }
    }
    void Update()
    {
        transform.LookAt(player);

        if (Vector3.Distance(transform.position, player.position) >= MinDist)
        {

            transform.position += transform.forward * Time.deltaTime;

            if (Vector3.Distance(transform.position, player.position) <= MaxDist)
            {
                Agent.SetDestination(player.position);
            }
        }
    }
}

Seb

Might help you at least debug this.

Try putting a log after you find the player, to see if the awake function is being called and actually getting the player.

Debug.log("Found Player? " + (player != null));

I would also check the position of your player transform in Update to make sure it’s moving.

Debug.log("player.position = " + player);

This might at least help you understand what is happening.

Thanks for your answer…

The script works, it’s just that the “Se promene” function is still doing it’s job. So the enemy goes to my player, but then random works again, so it goes away, then comes back, then goes away… How can i stop the SePromene function to stop when it tries to reach my player?

The InvokeRepeating method is calling Sepromene every 10 seconds, regardless of what happens in your update method, which is why it may feel random. You won’t really know what part of the update process it’s occurring.

InvokeRepeating("Sepromene", 1.0f, tempsdedetection);
//First invokes in 1 second, then every 10 seconds.

Documentation here

I’m not 100% sure what behavior you are trying to do, but here is a possible fix.
Perhaps you can declare a bool field called FollowPlayer = false.
When you want your enemy to continue, set it to true.

At the beginning of Sepromene , have it do this.

if ( FollowPlayer)
{
    return;
}

That way it will not run the Sepromene method.
Once you want your enemy to continue to patrol (if ever) set FollowPlayer = false;

Just an idea.