compoent Nav Mesh Agent
Agent Type : 네비게이션에 적용시킬 collider
Streering : 운전
static : navigation static
navigation tab => bake
using UnityEngine.AI;
public class nagivation : MonoBehaviour
{
Rigidbody MyRigid;
[SerializeField] private float moveSpeed;
[SerializeField] private Transform tf_Destination;
private Vector3 originPos;
NavMeshAgent agent;
void Start()
{
MyRigid = GetComponent<Rigidbody>();
agent = GetComponent<NavMeshAgent>();
originPos = transform.position;
}
private void Patrol()
{
if (Vector3.Distance(transform.position, tf_Destination.position) < 0.1f)
agent.SetDestination(originPos);
else if (Vector3.Distance(transform.position, originPos) < 0.1f)
agent.SetDestination(tf_Destination.position);
}
void Update()
{
Patrol();
}
}
using UnityEngine.AI;
public class navigation : MonoBehaviour
{
NavMeshAgent agent;
void Start()
{
agent = GetComponent<NavMeshAgent>();
}
void Update()
{
Collider[] col = Physics.OverlapSphere(transform.position, 10f);
if (col.Length > 0)
{
for (int i = 0; i < col.Length; i++)
{
Transform tf_Target = col[i].transform;
if(tf_Target.name == "Player")
{
NavMeshPath path = new NavMeshPath();
agent.CalculatePath(tf_Target.position, path);
Vector3[] wayPoints = new Vector3[path.corners.Length + 2];
wayPoints[0] = transform.position;
wayPoints[wayPoints.Length - 1] = tf_Target.position;
float _distance = 0f;
for(int j = 0; j < path.corners.Length; j++)
{
wayPoints[j + 1] = path.corners[j];
_distance += Vector3.Distance(wayPoints[j], wayPoints[j + 1]);
}
if (_distance <= 10f)
{
agent.SetDestination(tf_Target.position);
}
}
}
}
}
}