Unity
[유니티] 네이게이션
Skull Crusher
2021. 5. 20. 16:40
728x90
1.
SlimePBR 생성
2. 지도 만들기
Plane 생성 -> 바닥 이미지 넣기 -> Cube 2개 생성
3.
Window -> AI -> Navigation -> Bake -> Bake
4.
Slime Nav Mesh Agent 추가
C# Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//네비게이션 AI 사용
using UnityEngine.AI;
public class NavMeshMove : MonoBehaviour
{
//이동하려는 목적지 Transform
public Transform targetTrans;
//네브메시 에이전트 컴포넌트 참조
public NavMeshAgent navMeshAgent;
public bool isStop = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (!isStop)
{
//네브메시 에이전트로 이동 중
navMeshAgent.isStopped = false;
//네브메시 에이전트로 이동할 타겟 위치를 설정함
navMeshAgent.SetDestination(targetTrans.position);
}
else
{
navMeshAgent.isStopped = true;
}
}
}
5.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//네비게이션 AI 사용
using UnityEngine.AI;
public class NavMeshMove : MonoBehaviour
{
//이동하려는 목적지 Transform
public Transform targetTrans;
//네브메시 에이전트 컴포넌트 참조
public NavMeshAgent navMeshAgent;
//중지상태
public bool isStop = false;
public Animator animator;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (!isStop)
{
//네브메시 에이전트로 이동 중
navMeshAgent.isStopped = false;
//네브메시 에이전트로 이동할 타겟 위치를 설정함
navMeshAgent.SetDestination(targetTrans.position);
}
else
{
navMeshAgent.isStopped = true;
}
//이동 상태 여부에 따라 애니메이션을 재생함
animator.SetBool("IsWalk",isStop);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//네비게이션 AI 사용
using UnityEngine.AI;
public class NavMeshMove : MonoBehaviour
{
//이동하려는 목적지 Transform
public Transform targetTrans;
//네브메시 에이전트 컴포넌트 참조
public NavMeshAgent navMeshAgent;
//중지상태
public bool isStop = false;
public Animator animator;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (!isStop)
{
//네브메시 에이전트로 이동 중
navMeshAgent.isStopped = false;
//네브메시 에이전트로 이동할 타겟 위치를 설정함
navMeshAgent.SetDestination(targetTrans.position);
}
else
{
navMeshAgent.isStopped = true;
}
if (navMeshAgent.isStopped || (Vector3.Distance(targetTrans.position, transform.position) < 0.01f))
{
//이동 상태 여부에 따라 애니메이션을 재생함
animator.SetBool("IsWalk", false);
}
else
{
animator.SetBool("IsWalk", true);
}
}
}