2016-07-26 12 views
0

ユニティーエンジネのプレイヤーコントロールスクリプト(C#)に少し問題があります。私は、プレイヤーの基本的な動きで次のスクリプトを作成しました。問題は、プレイヤーがjumpステートメント(デバッグログに出力)を入力できることです。 Debug Log しかし、動作しません。キャラクターはまだ地上にいる。 ジャンプ機能は、プレーヤーが地面に接地しているときに有効になり、ダブルジャンプしませんでした。 私の質問は、 "コードミス"か、私には見えない構成上の問題があるかどうかです。キャラクターはUnity2Dでジャンプしませんが、ジャンプステートメントを入力しました

ご協力いただきありがとうございます。

using UnityEngine; 
using System.Collections; 
public class PlayerControl : MonoBehaviour 
{ 

    // public variables 
    public float speed = 3f; 
    public float jumpHeight = 5f; 

    // private variables 
    Vector3 movement; 
    Animator anim; 
    Rigidbody2D playerRigidbody; 

    // variables for the ground check 
    public Transform groundCheck; 
    public float groundCheckRadius; 
    public LayerMask whatIsGround; 
    private bool grounded; 
    private bool doubleJump; 

    // Use this for initialization 
    void Start() 
    { 

    } 

    // Update is called once per frame 
    void Update() 
    { 
     // Proves if the player is on the ground and activate the double jump function 
     if (grounded) 
     { 
      doubleJump = false; 
     } 

     // First line of proving the jump 
     if (Input.GetMouseButtonDown(0) && grounded) 
     { 
      Debug.Log("Jump if entered"); 
      Jump(); 
     } 

     if (Input.GetMouseButtonDown(0) && !doubleJump && !grounded) 
     { 
      Debug.Log("double Jump"); 
      Jump(); 
      doubleJump = true; 
     } 


     // Flipping the Player when he runs back 
     if (Input.GetAxis("Horizontal") < 0) 
     { 
      playerRigidbody.transform.localScale = new Vector2(-1.7f, 1.7f); 
     } 

     else 
     { 
      playerRigidbody.transform.localScale = new Vector2(1.7f, 1.7f); 
     } 
    } 

    void Awake() 
    { 
     // References setting up 
     playerRigidbody = this.GetComponent<Rigidbody2D>(); 
     anim = GetComponent<Animator>(); 
    } 

    void FixedUpdate() 
    { 
     float horizontal = Input.GetAxisRaw("Horizontal"); 
     float vertical = Input.GetAxisRaw("Vertical"); 

     // simple Movement without a speed control 
     Move(horizontal, vertical); 
     Animating(horizontal, vertical); 

     // Section for ground detection 
     grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround); 

     // Set the parameter for the jump animation false or true 
     anim.SetBool("Grounded", grounded); 
    } 

    void Move(float horizontal, float vertical) 
    { 
     movement.Set(horizontal, 0f, vertical); 
     movement = movement.normalized * speed * Time.deltaTime; 

     playerRigidbody.MovePosition(transform.position + movement); 
    } 

    void Jump() 
    { 
     playerRigidbody.AddForce(Vector3.up * jumpHeight); 
     // playerRigidbody.AddForce(new Vector2(0f, jumpHeight), ForceMode2D.Impulse); 
     Debug.Log("Jump function"); 
    } 

    void Animating(float h, float v) 
    { 
     bool walking = h != 0f || v != 0f; 
     anim.SetBool("IsWalking", walking); 
    } 
} 
+0

あなたのオブジェクトにはジャンプのアニメーションが有効になっていますか? –

+0

ご意見ありがとうございます。 問題はアニメーションではない、キャラクターは位置を変えなかった(ジャンプ)。アニメーションが動かないときには対処できますが、ポジションを変えないという問題はありません。 しかし、ここに別の奇妙なことがあります。私はこの小さなコードを 'void update()'関数に入れます: 'if(Input.GetKeyDown(KeyCode.Space)) playerRigidbody.AddForce(Vector2.up * jumpHeight);'これも機能しません。 – t3chnico

+0

別のアニメーションの動きをスペースで入れたら、それは機能しますか? また、私の主人公のオブジェクトでは、3人目のコントローラーを使用し、スペースを使ってジャンプすることができました。 –

答えて

0

ここでは推測していますが、おそらくVector3.upは2D物理では機能しません。私は実際に2Dにはなっていませんが、試してみることができます。

playerRigidbody.AddForce(transform.up * jumpHeight); 

また、jumpHeightに異なる値を試しましたか? 5あなたがあなたの剛体のために設定した質量に応じて、小さなものになるかもしれません。 そして、インスペクタで軸を制限していないことを確認してください。

+0

OMG! @タロクあなたは私の目を開いた! 昨日私はjumpHeigtのさまざまな設定を試しました - すべてが再生モードです。 あなたの偉大なヒントをありがとう。 – t3chnico

+0

@ t3chnico喜んで助けることができました;) – Tarrokk

0
// Note: If you want the object to move in a reliable predictable way but still allow physics interactions, use MovePosition (set the object to kinematic if you want it to be unaffected by physics but still be able to affect other things, and uncheck kinematic if you want both objects to be able to be acted on by physics. 

If you want to move your object but let physics handle the finer details, add a force. 

playerRigidbody.rigidbody2D.AddForce(Vector3.up * 10 * Time.deltaTime); 

use Vector.up, Vector.down, vector.right, Vectore.left along with time.deltaTime to have smooth movement along the frame. 
関連する問題