top of page

RayCastを飛ばす.非貫通(オブジェクト)

    void RayTest(){
        
        // outパラメータ用に、Rayのヒット情報を取得するための変数を用意
        RaycastHit hit;

        var isHit = Physics.Raycast (transform.position, transform.forward, out hit, 100);

        if (isHit) {
            
            Debug.DrawRay (transform.position, transform.forward * hit.distance);

            // Rayがhitしたオブジェクトのオブジェクト名を取得
            string hitName = hit.collider.gameObject.name;

            //ヒットしたオブジェクトの名前がTESTなら
            if (hitName.Equals("TEST")) 
            {
            }


            //TEST以外の名前なら
            else 
            {
            }

        }
        else

        {
            Debug.DrawRay (transform.position, transform.forward * 100);
        }

    }        
 

bottom of page