Unity

수업 내용 복습 6

psb08 2025. 3. 29. 02:28
728x90
반응형

RaycastTest.cs

using UnityEngine;

namespace RayTest
{
    public class RaycastTest : MonoBehaviour
    {
        [SerializeField] private float maxDistance = 10f;
        [SerializeField] private LayerMask whatIsEnemy;
        private void OnDrawGizmos()
        {
            RaycastHit hit;
        
            //bool isHit = Physics.Raycast(transform.position, transform.forward, out hit, maxDistance, whatIsEnemy);
            
            //bool isHit = Physics.BoxCast(transform.position, transform.lossyScale * 0.5f, transform.forward, out hit, transform.rotation, maxDistance);

            bool isHit = Physics.SphereCast(transform.position, transform.lossyScale.x * 0.5f, transform.forward, out hit,
                maxDistance);
            
            if (isHit)
            {
                Gizmos.color = Color.red;
                //Gizmos.DrawRay(transform.position, transform.forward * hit.distance);
                //Gizmos.DrawWireCube(transform.position + transform.forward * hit.distance, transform.lossyScale);
                Gizmos.DrawWireSphere(transform.position + transform.forward * hit.distance, transform.lossyScale.x * 0.5f);
                
            }
            else
            {
                Gizmos.color = Color.green;
                Gizmos.DrawRay(transform.position, transform.forward * maxDistance);
            }
        
        }
        
        
        
    }    
}

 

Ray를 총 3가지로 쏩니다. 앞에 물체가 없다면 길이만큼 선으로 레이를 쏩니다.

만약 물체가 맞았다면 순서대로 선, 박스, 구 형태로 색을 빨간색으로 레이를 쏩니다.

그리고 모형을 그려서 볼 수 있도록 합니다.

물체가 있을 때
물체가 없을 때

 

'Unity' 카테고리의 다른 글

2D, 3D 시점 변환 카메라로 만들어 보기  (0) 2025.04.14
수업 내용 복습 7  (0) 2025.03.29
수업 내용 복습 5  (0) 2025.03.18
수업 내용 복습 4  (0) 2025.03.17
수업 내용 복습 3  (0) 2025.03.16