본문 바로가기
Unity

[유니티] 네이게이션

by Skull Crusher 2021. 5. 20.
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);
        }
        
    }
}

 

 

'Unity' 카테고리의 다른 글

[유니티] 3D 슈팅게임 2  (0) 2021.05.27
[유니티] 3D 슈팅게임 만들기  (0) 2021.05.21
[유니티]UI (드롭박스)  (0) 2021.05.20
[유니티] UI/UX Samples  (0) 2021.05.18
[유니티] 조이패드  (0) 2021.05.18

댓글