我正在尝试旋转我的角色以面向鼠标位置,仅相对于变换:(0,0,0)

问题描述 投票:0回答:1

字符旋转,仅在鼠标围绕世界中心(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);
         }
     }
}
unity3d rotation quaternions raycasting
1个回答
0
投票

Quaternion.LookRotation将方向Vector作为参数而不是位置。

© www.soinside.com 2019 - 2024. All rights reserved.