2017-05-01 20 views
0

私はFPSプロジェクトを開始しました。私はプレイヤーとしてカプセルを持っています。空のGOは頭、カメラは頭の子オブジェクトです。ユニティでの垂直回転のクランプ

垂直軸では、y回転を-70(最小)と70(最大)に固定します。意外にも、値はクランプされません。

[SerializeField] 
    private Transform playerHead; 

    float inputHorizontal; 
    float inputVertical; 

    float sensitivity = 50; 

    float yMin = -70; 
    float yMax = 70; 

    Vector2 direction; // movement direction 

Transform playerTransform; 

void Start() 
{ 
playerTransform = GameObject.FindGameObjectWithTag("Player").transform; 
} 

    private void Update() 
    { 
     inputHorizontal = Input.GetAxisRaw("Mouse X"); 
     inputVertical = -Input.GetAxisRaw("Mouse Y"); // Inverted Input! 

     direction = new Vector2(
       inputHorizontal * sensitivity * Time.deltaTime, 
       Mathf.Clamp(inputVertical * sensitivity * Time.deltaTime, yMin, yMax)); // The horizontal movement and the clamped vertical movement 

     playerTransform.Rotate(0, direction.x, 0); 
     playerHead.Rotate(direction.y, 0, 0); 
    } 

direction.y 

が、私はまだ360度で私の頭の周りに回すことができます。..

答えて

2

デルタローテーションをクランプしています。実際のローテーションではありません。 別の言い方をすれば、directionは最終回転ではなく、回転の変化です。あなたが効果的にやっていることは、フレームあたりの回転を70度に制限しています。使用しているとき

Vector3 playerHeadEulerAngles = playerHead.rotation.eulerAngles; 
playerHeadEulerAngles.y = Mathf.Clamp(playerHeadEulerAngles.y, yMin, yMax); 
playerHead.rotation = Quaternion.Euler(playerHeadEulerAngles); 

方向ベクトルを作成する理由は、もありません:

あなたはおそらく更新の最後に次の行を追加することによって、例えば、playerHeadの実際の回転を制限したいですとにかく別々に各コンポーネント。

0

クランプ最終Y方向の値ではありません、現在のマウスの動きにクランプます -

[SerializeField] 
private Transform playerHead; 

float inputHorizontal; 
float inputVertical; 

float sensitivity = 50; 

float yMin = -70; 
float yMax = 70; 

Vector2 direction; // movement direction 
float currYDir = 0,prevYDir = 0; 

Transform playerTransform; 

void Start() 
{ 
playerTransform = GameObject.FindGameObjectWithTag("Player").transform; 
} 

private void Update() 
{ 
    inputHorizontal = Input.GetAxisRaw("Mouse X"); 
    inputVertical = -Input.GetAxisRaw("Mouse Y"); // Inverted Input! 

    direction = new Vector2(
      inputHorizontal * sensitivity * Time.deltaTime, 
      inputVertical * sensitivity * Time.deltaTime); // The horizontal movement and the clamped vertical movement 

    playerTransform.Rotate(0, direction.x, 0); 

    currYDir = prevYDir + direction.y; 
    currYDir = Mathf.Clamp(currYDir, yMin, yMax); 
    playerHead.Rotate(currYDir-prevYDir, 0, 0); 

    prevYDir = currYDir; 
} 
関連する問題