首页 > 代码库 > 【Unity笔记】将角色的碰撞体朝向鼠标点击方向——角色朝向鼠标

【Unity笔记】将角色的碰撞体朝向鼠标点击方向——角色朝向鼠标

int floorMask; // 自动寻路层
void Awake()
{
    floorMask = LayerMask.NameToLayer("Floor");
}
void Turning() { Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit floorHit; if (Physics.Raycast(camRay, out floorHit, floorMask)) { Vector3 playerToMouse = floorHit.point - transform.position; playerToMouse.y = 0; Quaternion newRotation = Quaternion.LookRotation(playerToMouse); playerRigidbody.MoveRotation(newRotation); } }

 

【Unity笔记】将角色的碰撞体朝向鼠标点击方向——角色朝向鼠标