我在Unity3D中使用FPS,我只想要Pitch和Yaw。一些移动后,我的鼠标外观脚本开始围绕z轴旋转(即ROLL)。如何制止?
这是我用于鼠标外观的脚本。
using UnityEngine;
using System.Collections;
public class MouseLooking : MonoBehaviour
{
float sensitivityX = 10f;
float sensitivityY = 10f;
public float minimumX = -360;
public float maximumX = 360;
public float minimumY = -60;
public float maximumY = 60;
public float delayMouse = 3;
public float noiseX = 0.1f;
public float noiseY = 0.1f;
public bool Noise;
private float rotationX = 0;
private float rotationY = 0;
private float rotationXtemp = 0;
private float rotationYtemp = 0;
private Quaternion originalRotation;
private float noisedeltaX;
private float noisedeltaY;
private float stunY;
private float breathHolderValtarget = 1;
private float breathHolderVal = 1;
private TouchScreenVal touch;
void Start ()
{
if (rigidbody)
rigidbody.freezeRotation = true;
originalRotation = transform.localRotation;
sensitivityX = sensitivityY = 10;
touch = new TouchScreenVal (new Rect (0, 0, Screen.width, Screen.height));
}
void Update ()
{
sensitivityY = sensitivityX;
// Screen.lockCursor = true;
stunY += (0 - stunY) / 20f;
if (Noise) {
noisedeltaX += ((((Mathf.Cos (Time.time) * Random.Range (-10, 10) / 5f) * noiseX) - noisedeltaX) / 100);
noisedeltaY += ((((Mathf.Sin (Time.time) * Random.Range (-10, 10) / 5f) * noiseY) - noisedeltaY) / 100);
} else {
noisedeltaX = 0;
noisedeltaY = 0;
}
#if UNITY_EDITOR
rotationXtemp += (Input.GetAxis ("Mouse X") * sensitivityX) + (noisedeltaX * breathHolderVal);
rotationYtemp += (Input.GetAxis ("Mouse Y") * sensitivityY) + (noisedeltaY * breathHolderVal);
rotationX += (rotationXtemp - rotationX) / delayMouse;
rotationY += (rotationYtemp - rotationY) / delayMouse;
#else
if (Constants.isGameOver == false) {
int count = Input.touchCount;
if (Input.touchCount > 0) {
for(int i =0 ; i< count ; i++)
{
Touch touch1 = Input.GetTouch (i);
if ((touch1.phase == TouchPhase.Moved ) || touch1.phase == TouchPhase.Stationary) {
rotationX = rotationXtemp + ( Input.GetTouch (i).deltaPosition.x * sensitivityX* Time.deltaTime) + (noisedeltaX * breathHolderVal);
rotationY = rotationYtemp + ( Input.GetTouch(i).deltaPosition.y * sensitivityY* Time.deltaTime) + (noisedeltaY * breathHolderVal);
// rotationX = rotationXtemp + (touch.OnDragDirection(true).x * sensitivityX * Time.deltaTime) + (noisedeltaX * breathHolderVal);
// rotationY = rotationYtemp + (-touch.OnDragDirection(true).y * sensitivityY * Time.deltaTime) + (noisedeltaY * breathHolderVal);
}
}
}
else
{
rotationX = rotationXtemp + (touch.OnDragDirection(true).x * sensitivityX * Time.deltaTime) + (noisedeltaX * breathHolderVal);
rotationY = rotationYtemp + (-touch.OnDragDirection(true).y * sensitivityY * Time.deltaTime) + (noisedeltaY * breathHolderVal);
}
}
#endif
if (rotationX >= 360) {
rotationX = 0;
rotationXtemp = 0;
}
if (rotationX <= -360) {
rotationX = 0;
rotationXtemp = 0;
}
rotationX = ClampAngle (rotationX, minimumX, maximumX);
rotationY = ClampAngle (rotationY, minimumY, maximumY);
rotationYtemp = ClampAngle (rotationYtemp, minimumY, maximumY);
Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
Quaternion yQuaternion = Quaternion.AngleAxis (rotationY + stunY, Vector3.left);
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
breathHolderVal += (breathHolderValtarget - breathHolderVal) / 10;
#if !UNITY_EDITOR
rotationXtemp = rotationX;
rotationYtemp = rotationY;
#endif
}
public void Holdbreath (float val)
{
breathHolderValtarget = val;
}
public void Stun (float val)
{
stunY = val;
}
static float ClampAngle (float angle, float min, float max)
{
if (angle < -360.0f)
angle += 360.0f;
if (angle > 360.0f)
angle -= 360.0f;
return Mathf.Clamp (angle, min, max);
}
}
答案 0 :(得分:0)
您不能使用浮点数来存储x和y旋转,您需要将它全部存储在四元数中。因此,当您从鼠标输入更新时,将其直接添加到四元数。