Hi everyone,
i have what seems a very small bug, but i can’t find the solution.
Here is the script:
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);
}
}
}
}
The function “Sepromene” is working great, so my enemy is walking randomly around. What i’d like is that when it comes near my player (void Update), it stops the SePromene function, but when my player is far, the function resumes.
Now what it does is that the enemy walks randomly, when it gets near my player it goes to my player’s position, but then starts again to go random, then goes to my player… I want it to stick with my player, unless my player goes far away then to resume the Sepromene function.
thanks a lot for your help
Seb