본문 바로가기
[ CODING STUDY ]/》유니티 실습

유니티 ai NavMeshAgent

by MRG 2024. 1. 20.
728x90
반응형

 

▣ 먼저 Packages에서 AI Navigation를 추가합니다. 

 

 

▣ Zombie 오브젝트에 Nav Mesh Agent Component 추가합니다. 

 

▣ Nav Mesh Agent Component를 통해서 Zombie 오브젝트 AI를 제어합니다. 

 

https://docs.unity3d.com/kr/2021.3/Manual/class-NavMeshAgent.html

 

내비메시 에이전트 - Unity 매뉴얼

NavMeshAgent 컴포넌트는 목표를 향해 움직일 때 서로를 피해가는 캐릭터 생성에 유용합니다. 에이전트는 내비메시를 이용하여 게임 월드에 대해 추론하고 서로 또는 기타 움직이는 장애물을 피할

docs.unity3d.com

 

 

▣ Zombie 스크립트를 생성해서 추가하고 스크립트에 AI에 대한 네임스페이스를 UnityEngine.AI 추가합니다. 

 

▣ AI에 모든 상태, 상황에 대한 변수를 정의해야 합니다. 위에 주석처럼 필요한 변수들을 선언합니다.

 

 

 

▣ Start() 함수에서 애니메이션, AI, 플레이어에 대한 정보를 대입합니다.

 

▣ Update() 함수에서는 시야 범위를 CheckSphere() 함수를 통해 체크하고 범위 들어오면 조건에 따라 상태를 호출합니다.

 

▣ CheckSphere() : 세계 좌표 position에 의해 정의된 구와 겹치는 충돌체가 있는 경우 true를 반환합니다

 

https://docs.unity3d.com/ScriptReference/Physics.CheckSphere.html

 

Unity - Scripting API: Physics.CheckSphere

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close

docs.unity3d.com

 

 

 

▣ OnDrawGizmosSelected() 함수를 사용하여 기지모에 색을 넣어서 공격범위, 시야범위를 그려주는 코드를 완성합니다. 

 

https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnDrawGizmosSelected.html

 

Unity - Scripting API: MonoBehaviour.OnDrawGizmosSelected()

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close

docs.unity3d.com

 

 

 

▣ Patroling() 함수 순찰 중 함수를 완성합니다. 

이동포인트가 도착했을 때 여부에 따라 이동하고 새로운 경로를 지정하고에 대한 코드를 완성했습니다.

 

▣ agent.speed는 ai에 이동 속도를 나타냅니다. 

 

▣ SetDestination() 함수는

목적지를 설정하거나 업데이트하여 새 경로에 대한 계산을 트리거합니다.

몇 프레임이 지나야 경로를 사용할 수 있습니다. 경로가 계산되는 동안 pathPending은 true입니다. 유효한 경로를 사용할 수 있게 되면 에이전트는 이동을 재개합니다.

 

https://docs.unity3d.com/ScriptReference/AI.NavMeshAgent.SetDestination.html

 

Unity - Scripting API: AI.NavMeshAgent.SetDestination

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close

docs.unity3d.com

 

▣ Vector3.magnitude 는 벡터의 길이를 반환합니다

현재 좀비에 위치와 이동포인트 위치를 빼서 그에 대한 길이를 구해 isWalkPoint 여부를 지정합니다. 

 

https://docs.unity3d.com/ScriptReference/Vector3-magnitude.html

 

Unity - Scripting API: Vector3.magnitude

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close

docs.unity3d.com

 

 

▣ 플레이어를 발견하면 쫓아가는 함수와 공격하는 함수입니다. 

 

▣ Invoke() 함수는  시간(초) 단위로 메서드를 호출합니다 

이 함수를 통해서 공격시간을 지정합니다.

 

 

https://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html

 

Unity - Scripting API: MonoBehaviour.Invoke

If time is set to 0 and Invoke is called before the first frame update, the method is invoked at the next Update cycle before MonoBehaviour.Update. In this case, it's better to call the function directly. Note: Setting time to negative values is identical

docs.unity3d.com

 

 

▣ Windows -> AI -> Navigation (Obsolete)를 추가하고 해당 Zombie오브젝트에서 Navigation 메뉴에 Bake로 AI 이동할 경로를 지정합니다. 

 

https://docs.unity3d.com/kr/2022.3/Manual/nav-BuildingNavMesh.html

 

내비메시 빌드 - Unity 매뉴얼

레벨 지오메트리에서 내비메시 생성 프로세스를 내비메시 베이킹이라 부릅니다.이 프로세스는 내비게이션 정적으로 마크된 렌더 메시와 모든 게임 오브젝트의 터레인을 수집한 후 처리하여 레

docs.unity3d.com

 

 

▣ 저장 후 플레이를 하고 스크립트 범위 및 포인트 위치를 이동하고 재생하면 AI가 코드에 맞게 작동합니다. 

728x90
반응형

'[ CODING STUDY ] > 》유니티 실습' 카테고리의 다른 글

유니티 night skybox  (0) 2024.01.21
유니티 배경음악, 레벨유지  (2) 2024.01.19
유니티 raycast, 총알효과  (1) 2024.01.19
유니티 3d ui  (0) 2024.01.18
유니티 뛰는 애니메이션  (0) 2024.01.18

댓글