Question by
mgravelat · Feb 12, 2019 at 12:30 AM ·
c#scripting problemscript.navmeshagentpatrol
Unity Navmesh agent patrol and chase player script issues.
I just began scripting for the first time and am completely stuck with trying to convert this script from a simple patrol script to a script where the enemy will leave the patrol and chase the player the second the player comes into his view. Once I figure this all out I can start working on making attack scripts and game state changes.
If anyone can tell me how they would do it or just send me a tutorial, I'd appreciate it. I have seen multiple patrol tutorials but combining the chase script I have with the ray and the patrol script seems to just keep failing. I have worked a few days on this problem but am running out of time and have to work on other parts of the assignment as well.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NPCSimplePatrol : MonoBehaviour {
//Dictates whether the agent waits on each node.
[SerializeField]
bool _patrolWaiting;
//The total time we wait at each node.
[SerializeField]
float _totalWaitTime = 3f;
//The probability of switching direction.
[SerializeField]
float _switchProbability = 0.2f;
//The list of all patrol nodes to visit.
[SerializeField]
List<Waypoint> _patrolPoints;
//Private variables for base behaviour.
UnityEngine.AI.NavMeshAgent _navMeshAgent;
int _currentPatrolIndex;
bool _travelling;
bool _waiting;
bool _patrolForward;
float _waitTimer;
// Use this for initialization
public void Start()
{
_navMeshAgent = this.GetComponent<UnityEngine.AI.NavMeshAgent>();
if (_navMeshAgent == null)
{
Debug.LogError("The nav mesh agent component is not attached to " + gameObject.name);
}
else
{
if (_patrolPoints != null && _patrolPoints.Count >= 2)
{
_currentPatrolIndex = 0;
SetDestination();
}
else
{
Debug.Log("Insufficient patrol points for basic patrolling behaviour.");
}
}
}
public void Update()
{
Debug.Log(_patrolWaiting);
//Check if we're close to the destination.
if (_travelling && _navMeshAgent.remainingDistance <= 1.0f)
{
_travelling = false;
//If we're going to wait, then wait.
if (_patrolWaiting)
{
_waiting = true;
_waitTimer = 0f;
}
else
{
ChangePatrolPoint();
SetDestination();
}
}
//Instead if we're waiting.
if (_waiting)
{
_waitTimer += Time.deltaTime;
if (_waitTimer >= _totalWaitTime)
{
_waiting = false;
ChangePatrolPoint();
SetDestination();
}
}
}
private void SetDestination()
{
if (_patrolPoints != null)
{
Vector3 targetVector = _patrolPoints[_currentPatrolIndex].transform.position;
_navMeshAgent.SetDestination(targetVector);
_travelling = true;
}
}
/// <summary>
/// Selects a new patrol point in the available list, but
/// also with a small probability allows for us to move forward or backwards.
/// </summary>
private void ChangePatrolPoint()
{
if (UnityEngine.Random.Range(0f, 1f) <= _switchProbability)
{
_patrolForward = !_patrolForward;
}
if (_patrolForward)
{
_currentPatrolIndex = (_currentPatrolIndex + 1) % _patrolPoints.Count;
}
else
{
if (--_currentPatrolIndex < 0)
{
_currentPatrolIndex = _patrolPoints.Count - 1;
}
}
}
}
Comment