Unity——射线检测

有方向,距离和层级的检测

 void Start () {
        num = LayerMask.GetMask("Default");
    }

    // Update is called once per frame
    void Update ()
    {     
        RaycastHit hit;
        //检测起始位置,检测方向,射线产生,检测距离,检测层数。
        bool isGround = Physics.Raycast(transform.position + Vector3.up * 0.1f, Vector3.down, out hit, dic, num);       
        if (isGround)
        {
            Debug.Log("Yes ");
        }
        else
        {
            Debug.Log("No");
        }
    }

有方向,距离的检测

        //右手坐标系
        Vector3 fwd = transform.TransformDirection(Vector3.left);
        if (Physics.Raycast(transform.position, fwd, 10))
        {
            print("There is someting in front of the object!");
        }

仅有范围的检测

  isGround = Physics.CheckSphere(transform.position, 0.2f);

猜你喜欢

转载自blog.csdn.net/qq_39710961/article/details/80076466