The mouse controls the character's rotation

Using the ray detection method, the main camera sends a ray to the position of the mouse

Declare variable private float camLeagth = 100;//The length of the ray
    //private LayerMask floorMask;//Number of levels on the ground
    private int floorMask;

To get the ground layer, write it in the Start method or the Awake method
        floorMask = LayerMask.GetMask("Floor");//Layer name, get the layer

Ray control rotation
        //Send a ray from the origin of the main camera to the position of the mouse
        Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        //If the ray detection is successful
        if(Physics.Raycast(camRay,out hit,camLeagth,floorMask))
        {             Debug.Log("Ray");             //Steer according to the mouse position             Vector3 playerMouse = hit.point - transform.position;//Vector calculation             rb.MoveRotation(Quaternion.LookRotation(playerMouse)) ;//rotate             //             //transform.LookAt(hit.point);         }






おすすめ

転載: blog.csdn.net/qq_57388481/article/details/127464667