我有一个主摄像头在 Unity3D 中,我想根据鼠标输入进行旋转,因此它可以作为第一人称视频游戏,您可以根据要查看的位置移动鼠标。
相机的起始值(Unity 中“检查器”选项卡中的“变换”选项卡)为:
-
Position:X = -1,Y = 1,Z = -11。
-
Rotation:X = 0,Y = 0,Z = 0。
-
Scale:X = 1,Y = 1,Z = 1。
我为主相机添加了一个脚本组件。它是以下类:
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);
}
}
}
但是,当我播放场景时,相机没有按应有的方式旋转。的价值观rotationx 轴、y 轴的变化,甚至z-axis.
我究竟做错了什么?
这是一个如何做的问题Quaternion
被计算。当修改多个轴时会发生这种情况。如果您注释所有 x 旋转或 y 旋转,并且一次仅在一个轴上旋转,您将意识到这个问题将会消失。
要通过鼠标输入正确旋转相机,请使用eulerAngles
or localEulerAngles
变量。这两者之间的选择取决于您正在做什么。
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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)