字符旋转,仅在鼠标围绕世界中心(0,0,0)旋转时发生 -
MoveToMouse()
完美运作,点击世界上的一个点,玩家移动,然后摄像机跟随。
但是当按住shift以将角色旋转到鼠标指向的位置时,它仅指向相对于世界中心的位置。
transform.LookAt
- 效果很好,但我希望能够平滑旋转。
using UnityEngine;
using UnityEngine.AI;
public class ClickToMove : MonoBehaviour
{
NavMeshAgent player;
public float rotSpeed = 10f;
void Start()
{
player = GetComponent<NavMeshAgent>();
}
void Update()
{
MoveToMouse();
LookAtMouse();
}
void MoveToMouse()
{
if (Input.GetMouseButtonDown(1))
{
RaycastHit hit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100))
{
player.destination = hit.point;
}
}
}
void LookAtMouse()
{
if (Input.GetKey(KeyCode.LeftShift))
{
RaycastHit lookHit;
Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out lookHit, 100);
//transform.LookAt - works perfectly, but want to be able to smooth the rotation.
//transform.LookAt(lookHit.point);
transform.rotation = Quaternion.Slerp(transform.rotation,
Quaternion.LookRotation(lookHit.point), rotSpeed * Time.deltaTime);
}
}
}
Quaternion.LookRotation将方向Vector作为参数而不是位置。