2016-07-23 3 views
0

学習練習として、私の "Player" GameObject(私のカメラの親である)に付けられたMouseLookAroundスクリプトを書いています。マウスの見た目がちらつくのはなぜですか?

using UnityEngine; 
using System; 

public class MouseLook : MonoBehaviour { 
    [Flags] 
    public enum RotationAxes 
    { 
     None, 
     LeftAndRight, 
     UpAndDown, 
     Both = LeftAndRight + UpAndDown 
    } 

    public RotationAxes axes = RotationAxes.Both; 
    public float horizontalSensitivity = 9f; 
    public float verticalSensitivity = 9f; 
    public float verticalMinimum = -45f; 
    public float verticalMaximum = 45f; 

    void OnGUI() 
    { 
     GUI.TextArea(new Rect(0, 0, 100, 20), "" + transform.localEulerAngles.x); 
    } 


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

     if ((axes & RotationAxes.LeftAndRight) != 0) 
     { 
      float yaw = Input.GetAxis("Mouse X") * horizontalSensitivity; 
      transform.Rotate(0, yaw, 0); 
     } 

     if ((axes & RotationAxes.UpAndDown) != 0) 
     { 
      float yaw = transform.localEulerAngles.y; 
      float pitch = -Input.GetAxis("Mouse Y") * verticalSensitivity; 
      //pitch = Mathf.Clamp(pitch, verticalMinimum, verticalMaximum); 
      pitch += transform.localEulerAngles.x; 
      transform.localEulerAngles = new Vector3(pitch, yaw, 0); 
     } 
    } 
} 

私はこのプロジェクトを実行して、アップ/ダウン私のマウスを移動すると、私は角度見下ろしたときに見上げたときに90に制限されて、そして私の角度がMathf.Clampがあるにもかかわらず(270に制限されていることに気付きますコメントアウト)。

さらに、ユニティがこれらの角度をクランプすると、ある程度のばらつきがあります。だから、私の視野角(ピッチ)がすでに90で、私のマウスをカメラの下に動かすと、89.98のようなものに変わり、場面全体がジャンプします。

誰かが私の範囲を制限している理由を説明してください。なぜこの吃音が発生するのですか?

+0

これは、変換の回転が数学的に定義されているためです。例えば。あなたが南極にいるなら、あなたは "南"に行きます、どちらの方向ですか? – Glurth

答えて

0

ソリューションは、今私のメンバー「ピッチ」というtransform.rotateを使用するよりもtransform.localEulerAnglesを設定すると、クラスメンバを持つようにピッチを格納するのではなくlocalEurlerAngles.x

using UnityEngine; 
using System; 

public class MouseLook : MonoBehaviour { 
    [Flags] 
    public enum RotationAxes 
    { 
     None, 
     LeftAndRight, 
     UpAndDown, 
     Both = LeftAndRight + UpAndDown 
    } 

    public RotationAxes axes = RotationAxes.Both; 
    public float horizontalSensitivity = 9f; 
    public float verticalSensitivity = 9f; 
    public float verticalMinimum = -45f; 
    public float verticalMaximum = 45f; 

    float pitch; 

    // Update is called once per frame 
    void Update() { 
     float yaw = transform.localEulerAngles.y; 
     if ((axes & RotationAxes.LeftAndRight) != 0) 
      yaw += Input.GetAxis("Mouse X") * horizontalSensitivity; 

     if ((axes & RotationAxes.UpAndDown) != 0) 
     { 
      float pitchDelta = -Input.GetAxis("Mouse Y") * verticalSensitivity; 
      pitch += pitchDelta; 
      pitch = Mathf.Clamp(pitch, verticalMinimum, verticalMaximum); 
     } 
     transform.localEulerAngles = new Vector3(pitch, yaw, 0); 
    } 
} 

を変更することができましたverticalMimimumとverticalMaximum(-45〜45)の間で変化します。この値を設定すると、水平方向に前進+その範囲の数値が得られます。

+0

それはあなたのちらつきの問題を解決しませんでした...それをしましたか? – Programmer

+0

はい、もうちらつきがなくなりました –

関連する問題