【问题标题】:Why when rotating an object with the mouse it's rotating to the opposite direction?为什么用鼠标旋转物体时它会旋转到相反的方向?
【发布时间】:2019-06-23 08:15:00
【问题描述】:
public class Rotate : MonoBehaviour
{
    public float speed = 1.0f;


    private void Update()
    {
        transform.Rotate(new Vector3(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0) * Time.deltaTime * speed);
    }
}

我想用鼠标平滑地旋转一个对象。

但在这种情况下,如果我将鼠标向右移动,它会将对象向左旋转,如果向上移动鼠标,它会向下旋转。

【问题讨论】:

  • 您是否尝试过在 X 和 Y 输入上都执行 * -1
  • @Joelius 这是有效的。但是,如果 Z 设置为 0 ,为什么它也会改变 Z 轴?为什么在玩游戏时用鼠标将 Z 校正回 0 很困难?
  • 您将 Z 硬编码为 0 我认为当您移动鼠标时这不会改变,但我对 Unity 不是很熟悉,所以我可能只是遗漏了一些明显的东西。
  • Z 值正在发生变化,因为将角度设置为欧拉角会导致转换为四元数(这是本机处理旋转的方式)。因此,这个四元数被 back 转换为欧拉角并显示在检查器中。因为欧拉和四元数表示不是 1:1,所以得到的欧拉 (a) 不保证相同,也不保证 (b) 在 Z 上为 0。

标签: c# unity3d


【解决方案1】:

这就像我想要的那样工作:

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

public class MouseOrbit : MonoBehaviour
{
    public float speedH = 2.0f;
    public float speedV = 2.0f;

    private float yaw = 180.0f;
    private float pitch = 0.0f;

    private void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    void Update()
    {
        yaw -= speedH * Input.GetAxis("Mouse X");
        pitch -= speedV * Input.GetAxis("Mouse Y");

        transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
    }
}

【讨论】:

    猜你喜欢
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 2017-01-19
    • 2023-02-25
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    相关资源
    最近更新 更多