AI NavMesh


바닥에 길찾기 가능한 구역을 설정해주자

Bake 를 하면 이 바닥에서는 길찾기가 가능하다는 범위가 표시된다
플레이어를 따라다닐 적 스크립트를 작성한다
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyAI : MonoBehaviour
{
public Transform player; // 플레이어를 추적
private NavMeshAgent agent;
void Start()
{
agent = GetComponent<NavMeshAgent>();
}
void Update()
{
if(player != null)
{
agent.SetDestination(player.position);
}
}
}

Enemy 가 될 오브젝트에 스크립트 컴포넌트를 넣고 , Nav Mesh Agent 컴포넌트를 넣고
따라다닐 대상을 넣어준다.

인게임 중 장애물이 생겼을때 인식해서 길을 찾는 방법

장애물에 Obstacle 컴포넌트 추가
Carve 체크



using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyAI : MonoBehaviour
{
public Transform player; // 플레이어를 추적
private NavMeshAgent agent;
void Start()
{
agent = GetComponent<NavMeshAgent>();
agent.SetDestination(player.position); // 실패 시 bool 반환
agent.destination = player.position; // 진짜 강제 주입 ( 위험한 코드 )
}
void Update()
{
if(player != null)
{
agent.SetDestination(player.position);
}
}
}
void Update()
{
if(player != null)
{
agent.SetDestination(player.position);
if(agent.isOnOffMeshLink)
{
// 애니메이션 점프 모션
}
}
}
'📖TIL' 카테고리의 다른 글
| 251117 (0) | 2025.11.17 |
|---|---|
| 251114 (0) | 2025.11.14 |
| 251113 스크립터블 오브젝트 (0) | 2025.11.13 |
| 251113 CSV & JSON (0) | 2025.11.13 |
| 251113 (0) | 2025.11.13 |