본문 바로가기
[ Unity ]/- 유니티 실습

유니티 2D AI 추적시스템

by MRG 2024. 9. 5.
728x90
반응형

 

▣ 코드 설명
player: 플레이어 캐릭터의 위치를 알아내기 위해 Transform을 참조합니다. Unity에서 Transform은 객체의 위치, 회전, 크기를 나타냅니다.


moveSpeed: 적 캐릭터가 플레이어를 따라갈 때의 속도입니다. 이 값을 조절하면 적의 이동 속도가 변경됩니다.
detectionRange: 적이 플레이어를 감지할 수 있는 범위를 설정하는 변수입니다. 이 범위 안에 플레이어가 들어오면 적이 추적을 시작합니다.


Update(): Unity에서 매 프레임마다 호출되는 메서드로, AI가 매 순간 플레이어의 위치를 추적하고 반응하는 로직을 여기에 작성합니다.


Vector2.Distance: 적과 플레이어 사이의 거리를 계산하는 데 사용됩니다. detectionRange와 비교하여 플레이어가 범위 안에 있는지를 판단합니다.


Vector2.MoveTowards: 현재 위치에서 목표 위치로 일정 속도로 이동하도록 하는 메서드입니다. 이 메서드를 사용해 적이 플레이어를 향해 자연스럽게 움직이도록 합니다.


OnDrawGizmosSelected(): 이 메서드는 Unity의 Gizmos 기능을 이용해 특정 객체를 선택했을 때만 기즈모가 그려지도록 합니다. 선택한 객체가 Scene 뷰에서 표시됩니다.


Gizmos.color: 기즈모의 색상을 설정합니다. 여기서는 빨간색으로 설정했습니다.


Gizmos.DrawWireSphere: 원형 기즈모를 그리기 위한 메서드로, 첫 번째 인자는 원의 중심 위치(여기서는 적의 위치), 두 번째 인자는 원의 반지름(여기서는 detectionRange)입니다. 이 메서드는 선으로만 된 원을 그립니다.

 

▣ 관련 API

https://docs.unity3d.com/ScriptReference/Transform.html

 

Unity - Scripting API: Transform

Every object in a Scene has a Transform. It's used to store and manipulate the position, rotation and scale of the object. Every Transform can have a parent, which allows you to apply position, rotation and scale hierarchically. This is the hierarchy seen

docs.unity3d.com

 

https://docs.unity3d.com/ScriptReference/Vector2.Distance.html

 

Unity - Scripting API: Vector2.Distance

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

https://docs.unity3d.com/ScriptReference/Vector2.MoveTowards.html

 

Unity - Scripting API: Vector2.MoveTowards

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

https://docs.unity3d.com/ScriptReference/Gizmos.html

 

Unity - Scripting API: Gizmos

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

https://docs.unity3d.com/ScriptReference/Gizmos.DrawWireSphere.html

 

Unity - Scripting API: Gizmos.DrawWireSphere

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

 

728x90
반응형

댓글