//======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
using UnityEngine;
using UnityEngine.AI;
///
/// Tells the attached NavMeshAgent component to walk to a random location, and finds a new location
/// each time it arrives.
/// Used in the ZED spatial mapping sample to make the bunny character walk around once you've finished
/// scanning your environment.
///
[RequireComponent(typeof(NavMeshAgent))]
public class RandomWalk : MonoBehaviour
{
///
/// Maximum distance of the next random point to walk to.
///
[Tooltip("Maximum distance of the next random point to walk to.")]
public float maxRange = 25.0f;
///
/// The NavMeshAgent component attached to this GameObject.
///
private NavMeshAgent m_agent;
///
/// Whether the agent should be walking.
///
private bool startWalking = false;
///
/// Current random destination the agent is walking toward.
///
private Vector3 destination;
///
/// Factor used to narrow the range of possible random destinations if positions at the range are difficult to find.
///
private uint reduceFactor = 0;
///
/// Enables the agent component and begins walking.
/// Called by EnemyManager once the agent is successfully placed.
///
public void Activate()
{
m_agent = GetComponent();
m_agent.enabled = true;
startWalking = true;
}
void Update()
{
if (startWalking)
{
if (m_agent.enabled)
{
if (m_agent.pathPending || m_agent.remainingDistance > 0.1f)
{
reduceFactor = 0;
return;
}
destination = (Mathf.Abs(maxRange) - reduceFactor) * Random.insideUnitSphere;
m_agent.destination = destination;
if(reduceFactor < Mathf.Abs(maxRange)/2) reduceFactor++;
}
}
}
}