-
04/15 유니티 화면 클릭으로 이동 구현 연습유니티 2021. 4. 15. 16:47
1. Hierarchy에 캐릭터와 맵 생성
2. window -> AI -> navigation
3. Bake탭에서 Baked Agent Size를 셋팅하고 Bake
4. 스크립트 작성 후 캐릭터에 컴포넌트 추가
5. 컴포넌트에 있는 agent속성에 캐릭터 어사인
새로운 agent Types을 사용하여 진행해 보려고 했으나, 이하의 에러가 발생하여 Humanoid로 진행
"SetDestination" can only be called on an active agent that has been placed on a NavMesh. UnityEngine.AI.NavMeshAgent:SetDestination (UnityEngine.Vector3) MoveTest:Update () (at Assets/Scripts/MoveTest.cs:32) "Resume" can only be called on an active agent that has been placed on a NavMesh. UnityEngine.StackTraceUtility:ExtractStackTrace () MoveTest:Update () (at Assets/Scripts/MoveTest.cs:31)
MoveTest.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class MoveTest : MonoBehaviour { public NavMeshAgent agent; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { if(Input.GetMouseButtonDown(0)) { var ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; Debug.DrawRay(ray.origin, ray.direction * 1000f, Color.red, 1f); if(Physics.Raycast(ray, out hit, 1000f)) { Debug.Log(hit.point); NavMeshHit navHit; var result = NavMesh.SamplePosition(hit.point, out navHit, 1f, NavMesh.AllAreas); Debug.Log(result); if (result) { Debug.Log("a"); agent.isStopped = false; agent.SetDestination(hit.point); } } } } }
'유니티' 카테고리의 다른 글
04/15 유니티 애니메이션 컨트롤러 연습 (0) 2021.04.16 04/15 유니티 LineRenderer 사용 연습 (0) 2021.04.15 04/15 유니티 IK 연습 (0) 2021.04.15 04/14 수업내용 메모 (0) 2021.04.14 04/13 유니티 쿠키런 제작 2 (0) 2021.04.13