Ray RayCastHit Physics.Raycast

void RotateMount()
    {
        RaycastHit rayCastHit;
        if (Input.GetMouseButtonDown(0))
        {
            if (mountGo.transform.FindChild("root_renwu").GetComponent<BoxCollider>() == null)
            {
                mountGo.transform.FindChild("root_renwu").gameObject.AddComponent<BoxCollider>().size = new Vector3(200, 200, 200);
            }  

            Ray ray = UICamera.mainCamera.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out rayCastHit, 100) && rayCastHit.collider.gameObject.name == "root_renwu")  
            {
                Debug.Log(rayCastHit.collider.name);
                Debug.DrawLine(ray.origin, rayCastHit.point);
                isHitMounter = true;
                rotationObj = rayCastHit.collider.gameObject;
            }
            else
            {
                isHitMounter = false;
            }
        }
    }
    GameObject rotationObj;
    void Update()
    {
        RotateMount();
        Debug.Log("" + isHitMounter);
        if (isHitMounter && Input.GetMouseButton(0))
        {
            if (Input.GetAxis("Mouse X") > 0)
            { 
                rotationObj.transform.Rotate(Vector3.up, 5, Space.Self);
            }
            if (Input.GetAxis("Mouse X") < 0)
            {
                rotationObj.transform.Rotate(Vector3.up, -5, Space.Self);
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/a133900029/article/details/79994360
今日推荐