2015-02-06 29 views
0

我在Unity3D中使用FPS,我只需要俯仰和偏航。在一些移動之後,我的Mouse Look Script開始圍繞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

有很多方法可以完成這項工作,而不是工作。你可以發佈你的代碼,以便我可以看到你目前正在做什麼? – sydan 2015-02-06 10:14:53

+0

請檢查更新 – 2015-02-06 10:52:57

回答