2017-08-30 13 views
0

私はUnityで2dゲームを作成しようとしています。私のゲームプレイヤーは、落書き軍のようないくつかの機能を持っています。私はプレイヤーの手を回転させて、45度の上下に限度を設け、両舞台軍プレイヤーのように上下に動かしたいと思う。私は2つのジョイスティックを移動のために使用し、2つ目はターゲットを目標に使用しています。したがって、第2のジョイスティックは、ジョイスティックの移動方向に手および頭を回転させることができる。私はこの回転を45度の上下両方向(上下左右)に制限したいと思います。プレーヤーの上下を45度の制限で回転させるには? unity 5

どれでもお手伝いできます。ここで

は2Dスプライト腕と頭を回転させるために私の小さなコードです。

using UnityEngine; 
using System.Collections; 
using UnityStandardAssets.CrossPlatformInput; 

public class PlayerRotation : MonoBehaviour { 

    public float moveForce = 5, boostMultiplier = 2; 
    GameObject ga; 

    // Use this for initialization 
    void Start() { 
     ga = GameObject.Find ("Charac_Head"); 
    } 

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

     Vector2 moveVec = new Vector2 (CrossPlatformInputManager.GetAxis ("Horizontal2"), 
           CrossPlatformInputManager.GetAxis ("Vertical2")) * moveForce; 



     //ga.transform.LookAt(transform.position.toVector2() + moveVec); 
      //ga.transform.Rotate (0, 0, moveVec.y); 
     if (moveVec.y <= 4.0f && moveVec.y >= -4.0f) { 
      //ga.transform.right = moveVec; 
      ga.transform.Rotate(0,0,moveVec.y); 
      Debug.Log (moveVec); 
     } 
     //} 
    } 

} 

答えて

1

Mathf.Clampを使用して、minとmaxの間の最終値を適用することができます。

Iなしテストコードが、私はあなたが必要なものを示しています。

moveVec.y = Mathf.Clamp(moveVec.y + Time.deltaTime*rotateRate, -45.0f, 45.0f); 
transform.localEulerAngles = moveVec; 
+0

私はこれをしようとします。 –