首先我们要在场景中加入一个quad平面作为地板, 然后指定Layer为Floor,然后移除mesh renderer组件
然后加入脚本
脚本主要思想是从屏幕中心投出一条射线到地板, 然后获取相应坐标,然后转化成角度
最后然角色平滑转到该角度就可以
using UnityEngine; public class PlayerMovement : MonoBehaviour { public float turnSmoothing = 5f; public float rayLength = 100f; private int floorMask; void Awake() { floorMask = LayerMask.GetMask("Floor"); } void FixedUpdate() { TurningWithMouse(); } void TurningWithMouse() { Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit floorHit; if (Physics.Raycast(camRay, out floorHit, rayLength, floorMask)) { Vector3 playerToMouse = floorHit.point - transform.position; playerToMouse.y = 0f; Quaternion targetRotation = Quaternion.LookRotation(playerToMouse, Vector3.up); Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime); rigidbody.MoveRotation(newRotation); } } }