Ssssong += Dev

[유니티] Raycast 본문

개발/공부

[유니티] Raycast

ssong_dev 2022. 5. 10. 13:07

 

레이캐스트 재밌당

 

 

Enemy 코드

void Update()
    {
        if(hp <= 0)
        {
            Destroy(gameObject);
        }

        RaycastHit hit;

        int layerMask = 1 << LayerMask.NameToLayer("Player");
        layerMask |= 1 << LayerMask.NameToLayer("Wall");

        bool rayhit = Physics.Raycast(transform.position, player.transform.position - transform.position, out hit, 4f, layerMask);
        Debug.DrawRay(transform.position, player.transform.position - transform.position, Color.red);

        Collider[] hitColliders = Physics.OverlapSphere(transform.position, 5f, layerMask);

        if(hitColliders.Length > 0 && rayhit && hit.collider.tag != "Wall")
        {
            Debug.Log(hitColliders.Length);
            rend.material.color = encounterColor;
        }
        else
        {
            rend.material.color = normalColor;
        }

    }

    private void OnDrawGizmos()
    {
        Gizmos.color = new Color(1, 1, 1, 0.3f);
        Gizmos.DrawSphere(transform.position, 5f);
    }