You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

111 lines
2.9 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
namespace Moloch
{
public class NMAgentManager
{
private GameObject m_Waypoint;
[HideInInspector]
public NavMeshAgent m_Agent;
public NavMeshPath m_CurrentPath;
private MeshRenderer m_floorBounds;
private GameObject m_NPC;
public NMAgentManager(NavMeshAgent agent, GameObject NPC, MeshRenderer floor)
{
this.m_Agent = agent;
this.m_NPC = NPC;
m_floorBounds = floor;
}
public void RandomWalk()
{
SetRandomDestination();
}
public bool HasReachedWaypoint()
{
if (m_Agent.remainingDistance <= 0.75f && !m_Agent.pathPending)
{
m_Agent.velocity = Vector3.zero;
return true;
}
else
{
return false;
}
}
public void SetSpeed(float maxSpeed)
{
m_Agent.speed = maxSpeed;
}
public void SetAcceleration(float maxAcc)
{
m_Agent.acceleration = maxAcc;
}
public float GetSpeed()
{
return m_Agent.speed;
}
public void MoveToWaypoint()
{
if (m_Waypoint != null)
{
m_Agent.SetDestination(m_Waypoint.transform.position);
StartMoving();
}
}
public void StopMoving()
{
m_Agent.isStopped = true;
}
public void StartMoving()
{
m_Agent.isStopped = false;
}
public void SetWaypoint(GameObject waypoint)
{
m_Waypoint = waypoint;
}
public void SetDestination(Vector3 position)
{
m_Agent.SetDestination(position);
}
public GameObject GetWaypoint()
{
return m_Waypoint;
}
private void SetRandomDestination()
{
float rx = Random.Range(m_floorBounds.bounds.min.x, m_floorBounds.bounds.max.z);
float rz = Random.Range(m_floorBounds.bounds.min.z, m_floorBounds.bounds.max.x);
Vector3 targetPosition = new Vector3(rx, m_NPC.transform.position.y, rz);
m_Agent.SetDestination(new Vector3(rx, m_NPC.transform.position.y, rz));
m_CurrentPath = m_Agent.path;
}
public void RotationOff()
{
//m_Agent.updateRotation = false;
var x = m_Agent.steeringTarget;
Vector3 direction = (x - m_Agent.transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(-direction.x, 0, -direction.z));
m_Agent.transform.rotation = Quaternion.Slerp(m_Agent.transform.rotation, lookRotation, Time.deltaTime * 10);
}
}
}