Unity Navigation ์ฌ์ฉ๋ฒ, NavMesh, NavAgent
๐ก ๊ณต๋ถํ ๋ด์ฉ ์ ๊ธฐ
Unity Navigation ์ฌ์ฉ๋ฒ Nav Mesh Agent, Obstacle, ์ฝ๋Permalink
navigation์ 3D๊ฒ์์์ ์๊ตฐ ๋๋ ์ ๊ตฐ์ด ๊ธธ์ ์ฐพ์๊ฐ๋๋ฐ ์ฐ์ด๋ package๋ก unity์์ ์ ๊ณตํ๋ค.
์ด ๊ธ์ Nav Mesh๋ก ํ ์ ์๋ ์ ํ ๊ณผ ์์ค์ฝ๋์ ๋ํด ์ ์๋ค.
์ด๋ฏธ ์ ์๋ ์ฌ๋์ ์ ๋ฆฌ๋ ๊ธ์ ์ฝ์ผ๋ฉด ๋๊ณ ์ฒ์ ์ ํ ์ฌ๋์ ์๋ ์์์ ๋ณด๋ฉด์ ๊ณต๋ถํ๋ฉด ์ข์ ๊ฒ ๊ฐ๋ค. ๋ฒ ๋ฅด์ ๊ฒ์ ๊ฐ๋ฐ - navigation์ค๋ช
๋๋ ์ฌ๊ธฐ ๋ณด๋ฉด์ ์ดํดํ ๊ฒ์ ์๋์ ์ ๋ฆฌํ๊ฒ์ด๋ค.
1. NavMeshAgentPermalink
์๋ ์ ํ ์ ์ฌ์ ์์ rigidbody์ Constraints๋ฅผ ๋ชจ๋ ๋ง๋๋ค
- Base Offset : ์ปฌ๋ผ์ด๋ ์ฒ๋ผ ๋ด ์ถฉ๋ ํ์ ํ๋๊ฑฐ ์์น ์ก๋๊ฑฐ์ธ๋ฏ
- Speed : ๋ชฉ์ ์ง๊น์ง ๊ฐ๋ ์๋
- Angular Speed : ๋ชฉ์ ์ง ๊ฐ๋ ๋ฐฉํฅ ๋๋ฆฌ๋ ์๋
- Acceleration : ์ต๋ ์๋ ๋๋ฌ ์๋ ๋ฐ๋ก ๋ฐ๋ก ์์ง์ด๋ ค๋ฉด 1000 ์ด๋ ๊ฒ ์ฃผ๋๊ฒ๋ ์ข๋ค
- Stopping Distance : ๋ชฉ์ ์ง ์ผ๋ง๋ ์ ์ ๋ฉ์ถ๊ฒ์ธ์ง, range ์ ๋์ฉ
- Auto Braking : ๋ชฉ์ ์ง ๋์ฐฉ์ ๋ถํฐ ๊ฐ์ํ ๊ฒ์ธ์ง, ์ฒดํฌ๋ฅผ ํ๋ฉด ๋ชฉ์ ์ง๋ฅผ ์ง๋์ณ๊ฐ ๊ฐ ๊ฐ๋ค์ ์๊ฐ ๋ณด๋ค ํฌ๊ฒ ์ก์์ผํ ๋ฏ
- Radius : ๋ค๋ฅธ ์ ๋๋ค์ ๋ฐ์ณ๋ด๋ ๋ฒ์, ๋ฒฝ๋ค์ ๊ฑธ๋ฆฌ์ง๋ ์์
- Height : ๋ค๋ฅธ ์ ๋์ ๋ฐ์ณ๋ด๋ ๋์ด
- Quality : ๋ค๋ฅธ ์ ๋์ ํผํ๋ ์์ง์์ ์ ๋ฐ๋ก ์กฐ์
- Priority : ์ฐ์ ์์๊ฐ ๋์ agent๋ ์ฐ์ ์์๊ฐ ๋ฎ์ agent๋ฅผ ๋ฌด์ํ๊ณ ์ง๋๊ฐ, ๋ฎ์ ์ฐ์ ์์๋ ๋์ ์ฐ์ ์๋ฅผ ๋ฐ์ง ๋ชปํจ
- Auto Traverse Off Mesh : โ
- Auto Repath : mesh๋ฑ์ ์ํฉ์ด ๋ณํ ๋๋ง๋ค ์๋ก ๊ธธ์ ์ฐพ์(๊ฒฝ๋ก๋ฅผ ์ฌ๊ณ์ฐ), ํ์ง๋ง ์์ผ์ ๋ณด์ด์ง ์์๋ ๋ฐ๋ก๋ฐ๋ก ์ฐพ๊ธฐ ๋๋ฌธ์ ์์ผ์ ๋ณด์ด์ง ์๋๊ณณ๊น์ง ์ด๋ํ๋ค ๋ชป์์ง์ด๋ ์ํฉ์ ๋ค๋ฅธ action์ ํ๊ณ ์ถ๋ค๋ฉด ์ฒดํฌ๋ฅผ ํด์ ํ๊ณ ์ฝ๋๋ก ์น๋ถ๋ฅผ ๋ด์ผํจ
- Area Mask : ๊ฐ์ข ํ์ ๋ค์ด ์ ํด์ ธ์๊ณ , ์๋ฅผ ๋ค์ด ๋ฌผ๋ก ๋ชป๊ฐ๋ ์ ๋์ ๋ฌผ์ ์ ํํด์ ํ๋ฉด ๊ทธ์ชฝ์ผ๋ก ๊ฐ์ง ์๋๋ค.
2. NavMesh ObstaclePermalink
NavMesh agent๊ฐ ์๋ obstacle
Navgation static(๋ฒฝ ๋ฑ) ์ผ๋ก ์ค์ ๋๊ฒ๊ณผ navmesh obstacle๋ก ์ค์ ๋๊ฒ์ ๊ฐ์ฅ ํฐ์ฐจ์ด๋ obstacle์ ๊ฒ์ ์ค์ ์์ง์ผ ์ ์๋ ์ฅ์ ๋ฌผ์ด๋ค.
- **Carve ** : ์ค์๊ฐ์ผ๋ก navmesh ์์น๋ฅผ ํ๋ด์ ์ง๋๊ฐ ์ ์๋ ์์ญ์ผ๋ก ๋ง๋๋ ๊ฒ
-
Move Threshold : Threshold๋ณด๋ค ์ ๊ฒ ์์ง์ธ๊ฒ์ ์๋ก ์์ง์ธ๊ฑธ๋ก ํ๋จํ์ง ์์์ Carve๋ฅผ ์๋ก ๊ทธ๋ฆฌ์ง ์์
- Time to Stationary : ์์ง์์ ๋ฉ์ถ๊ณ navmesh๋ฅผ ์๋ก ๊ณ์ฐํ๋๋ฐ ์ด๊ฑด 0.5์ด ํ์ ๊ณ์ฐํ๋๋๋ป
- Carve Only Stationary : ์ฒดํฌ ๋ฐ์ค๋ฅผ ๋๋ฉด ์์ง์ผ ๋๋ง๋ค ์๋ก ๊ณ์ฐํ๋๋ฐ, ์ด๋ฌ๋ฉด ๋ ์์ฐ์ค๋ฝ์ง๋ง ์ฑ๋ฅ์ ์์ข๋ค. ๊ทธ๋์ ๋ฐ๋์ ํ์ํ๊ณณ์์๋ง ๋์. ๋ฐ๋ก ์์ ์ต์ ๊ณผ ๊ด๋ จ์ด ์์
3. ํด๋ฆญํ ์์น๋ก ์บ๋ฆญํฐ ์ด๋Permalink
๋ฒ ๋ฅด์ ๊ฒ์ ๊ฐ๋ฐ - ํด๋ฆญํ ์์น๋ก ์บ๋ฆญํฐ ์ด๋
a. NavAgent๋ฅผ ์์ด ์ฝ๋Permalink
- ๋ง์ฐ์ค ํฐ์น ์ Raycast๋ก ๋ชฉ์ ์ง point๋ฅผ SetDestination()
- move ๋ณ์ true ๋ฐ ์ ๋๋ฉ์ด์ ๋ณ๊ฒฝ
- move() ํจ์ ์ง์์ update ๋ชฉํ ์์น ๋๋ฌ ์ move = false ๋ฐ animation ๋ณ๊ฒฝ
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
if (Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), out hit))
{
SetDestination(hit.point);
}
}
Move();
}
private void SetDestination(Vector3 dest)
{
destination = dest;
isMove = true;
SetAnimation(ANIMATION_QUADRUPED_WALK);
}
private void Move()
{
if (isMove)
{
var dir = destination - transform.position;
transform.forward = dir; // ๋ฐ๋ผ๋ณด๋ ๋ฐฉํฅ
transform.position += dir.normalized * Time.deltaTime * 5f;
}
if (Vector3.Distance(transform.position, destination) <= 1f)
{
isMove = false;
SetAnimation(ANIMATION_IDLE_C);
}
}
private void SetAnimation(int what)
{
animator.SetInteger("animation", what);
}
b. NavAgent๋ฅผ ์ด ์ฝ๋Permalink
- agent.updateRotation = false : ๋ฐฉํฅ์ ์ง์ ์ ํด์ค๊บผ๋ผ ํ์์์
- ์ง์ ์์น๋ฅผ ๋ฐ๊พธ๋ ์ฝ๋๋ฅผ agent.SetDestination์ผ๋ก ๋ณ๊ฒฝ
- ๋ณด๋ ๋ฐฉํฅ์ agent.steeringTarget๋ฅผ ์ด์ฉํ๋๋ก ๋ณ๊ฒฝ
agent.updateRotation = false; // ์บ๋ฆญํฐ๊ฐ ๋ณด๋ ๋ฐฉํฅ์ ๋ด๊ฐ ์ง์ ์ง์ ํด ์ค๊ฑฐ๋๊น ๊ตณ์ด ๋กํ
์ด์
์ ๋ฐ๊ฟ ํ์ ์์
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
if (Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), out hit))
{
SetDestination(hit.point);
}
}
LookMoveDirection();
}
private void SetDestination(Vector3 dest)
{
destination = dest;
agent.SetDestination(destination);
isMove = true;
SetAnimation(ANIMATION_QUADRUPED_WALK);
}
private void LookMoveDirection()
{
if (isMove)
{
if (agent.velocity.magnitude == 0f)
{
isMove = false;
SetAnimation(ANIMATION_IDLE_C);
return;
}
var dir = new Vector3(agent.steeringTarget.x, transform.position.y, agent.steeringTarget.z) - transform.position;
transform.forward = dir;
}
}
private void SetAnimation(int what)
{
animator.SetInteger("animation", what);
}
๊ธ์์ sample
fire brick
violet
drak green
medium blue
dark quoise
fire brick
violet
drak green
medium blue
dark quoise
๐ ํฌ์คํ
๊ณต์ง
ใ
ใ
ใดใปใดใ