`Unity Quaternion RotateTowards()` 中文翻译:`Unity 四元数 RotateTowards()`

huangapple go评论46阅读模式
英文:

Unity quaterion rotatetoward()

问题

目前,我正在使用鼠标为Unity相机制作第一人称视角。相机应该朝着鼠标的方向移动。

在代码中,我正在尝试理解Quaternion.RotateTowards()是如何工作的。RotateTowards有三个参数:起始角度,目标角度和旋转速度。

所以,在代码中,通过inputAction,我收到来自鼠标的Vector2的x和y值。例如,如果我只将鼠标向右移动,值会是(1.0,0),(2.0)……如果我将鼠标向上移动(0,1.0),(0.3)……然后,当我将鼠标移动到对角线时,值为(1.0,1.0)……

因此,使用Quaternion.RotateTowards()实际上会使相机旋转到我想要的位置。但问题是它不是根据我设置的速度(20f)进行旋转。

我知道使用
transform.rotation = goalRotation;
是解决问题的更简单方法,而不是使用RotateTowards(),但是我想了解这个函数应该如何工作。

有关此问题的任何想法吗?

英文:

Right now, I am working on the Unity camera for first person view using mouse.
Camera is suppose to move toward to direction of mouse.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

namespace input
{
    public class CameraControl : MonoBehaviour
    {
        [SerializeField] private GameObject player;
        [SerializeField] private float speed = 20f;

        float xRotation;
        float yRotation;

        private InputAction cameraAction;
      
        public void Initialize(InputAction cameraAction)
        {
            
            this.cameraAction = cameraAction;
            this.cameraAction.Enable();
        }

        private void FixedUpdate()
        {
            Vector2 camera = cameraAction.ReadValue<Vector2>();
          
            float mouseX = camera.x;
            float mouseY = camera.y;

            xRotation -= mouseY;
            yRotation += mouseX;

            Quaternion goalRotation = Quaternion.Euler(xRotation, yRotation, 0);

            //Quaternion goalRotation = Quaternion.Euler(30, 30, 0);
            //Debug.Log(goalRotation);
            //Quaternion goalRotation = Quaternion.LookRotation(direction);

            transform.rotation = Quaternion.RotateTowards(transform.rotation, goalRotation, speed * Time.deltaTime);
            //transform.rotation = goalRotation;
            player.transform.rotation = Quaternion.Euler(0, yRotation, 0);
            
        }
    }

}

In the code, I am trying to understand how Quaternion.RotateToward() is working. RotateToward is having tree parameters: target from, target to, and speed of rotation.
Target from is current roation of camera. Target to is rotation that camera should rotate.

So, in the code, from the inputAction, I am receiving the Vector2 x and y values from mouse.
For example, if I move the mouse only to the right value would be like (1.0, 0) (2.0) ...
and if I move the mouse to upward (0, 1.0) (0.3)... Then, when I move the mouse diagnal, (1.0 1.0)..

So, using the Quaternion.RotateToward() actually rotate to the point that I want. However, problem is that it is not rotating based on the speed that I set which is 20f.

I know using

transform.rotation = goalRotation;

is eaier way to solve rather than use RotateToward(), however, I want to learn how this function should be working.

Any idea about this?

答案1

得分: 0

speed * Time.deltaTime 变成 speed 能够解决问题。

英文:

speed * Time.deltaTime to speed ables to solve the problem

huangapple
  • 本文由 发表于 2023年2月16日 13:03:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468045.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定