为什么在Unity3D中摄像机绕z轴旋转? [英] Why is a camera rotated around z-axis in Unity3D?

查看:758
本文介绍了为什么在Unity3D中摄像机绕z轴旋转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Unity3D中有一个主摄像头,我想根据鼠标输入对其进行旋转,因此它可以作为第一人称电子游戏,您可以根据要看的位置移动鼠标在.

I have a main camera in Unity3D that I want to rotate depending on mouse input, so it works as a first person video-game where you move the mouse depending on where do you want to look at.

摄像机的起始值(Unity中检查器"选项卡中的变换"选项卡)为:

The starting values of the camera (Transform tab in Inspector tab in Unity) are:

  1. 位置:X = -1,Y = 1,Z = -11.
  2. 旋转:X = 0,Y = 0,Z = 0.
  3. 比例:X = 1,Y = 1,Z = 1.
  1. Position: X = -1, Y = 1, Z = -11.
  2. Rotation: X = 0, Y = 0, Z = 0.
  3. Scale: X = 1, Y = 1, Z = 1.

我为主摄像机添加了一个脚本组件.这是下面的类:

I added a Script component for the Main Camera. And it is the following class:

using UnityEngine;
using System.Collections;

public class CameraMove : MonoBehaviour {

     float deltaRotation = 50f;

     // Use this for initialization
     void Start () {

     }

     // Update is called once per frame
     void Update () {

          if(Input.GetAxis("Mouse X") < 0){
          //Code for action on mouse moving left
               transform.Rotate (new Vector3 (0f, -deltaRotation, 0f) * Time.deltaTime);
          }
          else if(Input.GetAxis("Mouse X") > 0){
          //Code for action on mouse moving right
               transform.Rotate (new Vector3 (0f, deltaRotation, 0f) * Time.deltaTime);
          }

          if(Input.GetAxis("Mouse Y") < 0){
          //Code for action on mouse moving left
               transform.Rotate (new Vector3 (deltaRotation, 0f, 0f) * Time.deltaTime);
          }
          else if(Input.GetAxis("Mouse Y") > 0){
          //Code for action on mouse moving right
               transform.Rotate (new Vector3 (-deltaRotation, 0f, 0f) * Time.deltaTime);
          }
     }
}

但是,当我播放场景时,相机并没有像预期的那样旋转. 旋转的值在x轴,y轴甚至 z轴中都发生变化.

However, when I play the scene the camera doesn't rotate like it should. The values of the rotation change in x-axis, y-axis and even for z-axis.

我在做什么错了?

推荐答案

Quaternion的计算方式存在问题.修改多轴时会发生这种情况.如果注释所有x旋转或y旋转,并且一次仅沿一个轴旋转,您将意识到此问题将消失.

That's a problem with how Quaternion is calculated. This happens when multiple axis are being modified. If you comment all the x rotation or the y rotation, and only rotate in one axis at a time, you will realize that this problem will go away.

要使用鼠标输入正确旋转相机,请使用eulerAngleslocalEulerAngles变量.两者之间的选择取决于您在做什么.

To properly rotate your camera with the mouse input, use the eulerAngles or localEulerAngles variables. The option between these two depends on what you are doing.

public float xMoveThreshold = 1000.0f;
public float yMoveThreshold = 1000.0f;

public float yMaxLimit = 45.0f;
public float yMinLimit = -45.0f;


float yRotCounter = 0.0f;
float xRotCounter = 0.0f;


// Update is called once per frame
void Update()
{
    xRotCounter += Input.GetAxis("Mouse X") * xMoveThreshold * Time.deltaTime;
    yRotCounter += Input.GetAxis("Mouse Y") * yMoveThreshold * Time.deltaTime;
    yRotCounter = Mathf.Clamp(yRotCounter, yMinLimit, yMaxLimit);
    transform.localEulerAngles = new Vector3(-yRotCounter, xRotCounter, 0);
}

这篇关于为什么在Unity3D中摄像机绕z轴旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆