SphereCast & LayerMask

Unity3D 2022. 3. 31. 14:56
반응형

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor.PackageManager;
using UnityEngine;

public class Player : MonoBehaviour
{
    private CapsuleCollider col;

    private Vector3 origin;

    private Vector3 direction;

    private float currentHitDistance;

    public float sphereRadius = 1.0f;
    public float maxDistance = 5;
    
    [SerializeField]
    private LayerMask layerMask;
    // Start is called before the first frame update
    void Start()
    {
        this.col = this.GetComponent<CapsuleCollider>();


        /*
         *
         * https://dallcom-forever2620.tistory.com/18
         * 
         * //특정 Layer만 raycast하기
            int layerMask = 1 << LayerMask.NameToLayer("Player");  // Player 레이어만 충돌 체크함
            Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), hit, Mathf.Infinity, layerMask);
             
            //두개 레이어 raycast하기
            int layerMask = (1 << LayerMask.NameToLayer("Player")) + (1 << LayerMask.NameToLayer("MyTeammate"));    // Player 와 MyTeammate 레이어만 충돌체크함
            Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), hit, Mathf.Infinity, layerMask);
             
             
            //특정 layer만 raycast제외하기 (1)
            int layerMask = (-1) - (1 << LayerMask.NameToLayer("Player"));  // Everything에서 Player 레이어만 제외하고 충돌 체크함
            Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), hit, Mathf.Infinity, layerMask);
             
             
            //특정 layer만 raycast제외하기 (2)
            int layerMask = (1 << LayerMask.NameToLayer("Player"));  // Everything에서 Player 레이어만 제외하고 충돌 체크함
            layerMask  = ~layerMask ;
            Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), hit, Mathf.Infinity, layerMask);

            //특정 2개이상 layer raycast 제외하기 
            int layerMask = ((1 << LayerMask.NameToLayer("Player")) | (1 << LayerMask.NameToLayer("GUN")));  // Everything에서 Player,GUN 레이어만 제외하고 충돌 체크함
            layerMask  = ~layerMask ;
            Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), hit, Mathf.Infinity, layerMask);
             
         */
        this.layerMask = 1 << LayerMask.NameToLayer("Hitable");
    }

    // Update is called once per frame
    void Update()
    {
        this.origin = this.col.bounds.center;
        this.direction = this.transform.forward;
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = new Ray(this.origin, this.direction);
            RaycastHit hit;
            if (Physics.SphereCast(ray.origin, this.sphereRadius, ray.direction, out hit, this.maxDistance, layerMask, QueryTriggerInteraction.UseGlobal))
            {
                this.currentHitDistance = hit.distance;
            }
            else
            {
                this.currentHitDistance = this.maxDistance;
            }
        }
    }

    private void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Debug.DrawLine(this.origin, this.origin + this.direction * this.currentHitDistance);
        Gizmos.DrawWireSphere(this.origin + this.direction * this.currentHitDistance, this.sphereRadius);
    }
}

 


 

참고 

https://www.youtube.com/watch?v=Nplcqwq_oJU 

https://dallcom-forever2620.tistory.com/18

 

반응형
: