Unity/엔진프로그래밍

0415_좀비슈터 단계별 테스트 _ RayCast 사용

minquu 2021. 4. 14. 11:18
반응형

0. 우먼캐릭터 세팅 및 맵 꺼내오기

 

1. NavMesh 맵에 만들기 

 

2. App 스크립터 생성 후 레이를 쏘고, 잘 나오는지 확인을 함

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class App : MonoBehaviour
{
    public Transform point;
    public NavMeshAgent agent;
    public Zombie zombie;
    // 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);
        }
    }
}

 

 

 

3. 레이와 닿는 곳에(Hit) 포인트 포지션 달아주기

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class App : MonoBehaviour
{
    public Transform point;
    public NavMeshAgent agent;
    public Zombie zombie;
    // 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);
                this.point.transform.position = hit.point;
            }
        }
    }
}

if(Physics.Raycast(ray, out hit, 1000f))

구문은 Raycast는 부울 값을 반환하는 키워드이다. 

먼저 ray는 카메라에서 쏜 Ray를 뜻하고

out hit 는 닿는 부분의 값을 내보내는 곳이다.

1000f는 최대 레이의 길이다.

 

현재는 부딪친 hit를 내보내고 있는 역할로 크게 쓰이고있다.

 

Raycast에 들어가는 값은 다양하게 달라질수도 있다.

 

-참고 블로그

[Unity3D] 레이캐스트(Raycast) :: 싸비 (tistory.com)

 

[Unity3D] 레이캐스트(Raycast)

레이캐스트(Raycast) 레이캐스트는 광선을 쏘는 것을 의미합니다. 여기서는 레이를 쏜다 라고 표현하겠습니다. 레이캐스트를 사용하면 광선에 충돌되는 콜라이더(Collider)에 대한 거리, 위치 등의

ssabi.tistory.com

 

4. Nav 바닥에 빈곳인지 아닌지 알아내고, 움직이기

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class App : MonoBehaviour
{
    public Transform point;
    public NavMeshAgent agent;
    public Zombie zombie;
    // 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);
                this.point.transform.position = hit.point;
                NavMeshHit navHit;
                var result = NavMesh.SamplePosition(hit.point, out navHit, 1.0f, NavMesh.AllAreas);
                Debug.Log(result);
                if (result) {
                    agent.isStopped = false;
                    agent.SetDestination(hit.point);
                }
            }
        }
    }
}

 

   NavMeshHit navHit;
                var result = NavMesh.SamplePosition(hit.point, out navHit, 1.0f, NavMesh.AllAreas);

 

NavMesh.SamplePosition 은 부울 값을 반환한다. 

 

이런 구조로 되어있다.

먼저 소스 포인트를 넣고, 

out으로 결과의 위치를 빼주고, 

거리를 넣고

특정 지역의 마스크인지 아닌지 비교한다.

 

true면 그 곳에 Navmesh가 있는 곳이라는 것이다.

 

                if (result) {
                    agent.isStopped = false;
                    agent.SetDestination(hit.point);
                    

 

따라서 result가 투르면

 

agent.isStopped 를 false로 만들고

agent.SetDestination(hit.point)  // agent의 이동을 목표지점(hit.point)로 이동시킨다.

 

 

 

 

 

 

 

 

반응형