2017-04-12 20 views
0

ユニティに関するヘルプフォーラムを見ると、私が見ていた構文が本当に時代遅れであることがわかりました(同じことがここにあります:Unity Doublejump in C#)。Unity2Dでのダブルジャンプ制限

は、ここで私が話していた記事です。たとえば http://answers.unity3d.com/questions/753238/restrict-number-of-double-jumps.html

、無効覚醒()で、私が使用しているユニティの現在のバージョンでは、それはrigidbody2D.fixedAngle = trueのことを言います。もはやサポートされなくなりました。プログラムしようとしていたgameObjectに制約を適用する必要がありました(x軸、y軸またはz軸はどの軸を使うべきですか)。いくつかの編集を行い、エラーメッセージを調べた後、私はrigidbody2D.velocityのすべてをGetComponent().velocityという更新された構文に変更することができました。

ここに私のコードです:

using System.Collections; 
    using System.Collections.Generic; 
    using UnityEngine; 

    public class NewBehaviourScript : MonoBehaviour { 

public float speed = 6.0f; 
//public float j 
Transform groundCheck; 
//private float overlapRadius = 0.2f; 
public LayerMask whatisGround; 
private bool grounded = false; 
private bool jump = false; 
public float jumpForce = 700f; 
private bool doubleJump = false; 
public int dJumpLimit = 5; 

void Start() 
{ 
    groundCheck = transform.Find ("groundcheck"); 
    PlayerPrefs.SetInt ("doublejumps", dJumpLimit); 
} 

void Update() 
{ 
    if (Input.GetKey(KeyCode.Space)) 
    { 
     jump = true; 
    } 
    //perhaps put A and D here? 
} 
void FixedUpdate() 
{ 
    //to check if Mario is on the ground 

    //overlap collider replace Overlap Circle??? 
    //overlap point?? 
    //grounded = GetComponent<Rigidbody2D>().OverlapCollision(groundCheck.position, overlapRadius, whatisGround); 

    if (grounded) 
     doubleJump = false; 

    if (dJumpLimit < 1) 
     doubleJump = true; 

    bool canJump = (grounded || !doubleJump); 

    if (jump && canJump) { 
     GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, 0); 
     GetComponent<Rigidbody2D>().AddForce (new Vector2 (0, jumpForce)); 

     if (!doubleJump && !grounded) { 
      doubleJump = true; 
      dJumpLimit--; 

     } 

    } 

     jump = false; 


     //code that will work with the limits? 
     //GetComponent<Rigidbody2D>().velocity = new Vector2(speed,GetComponent<Rigidbody2D>().velocity.y); 

     //this will make it stack? 
     //GetComponent<Rigidbody2D>().velocity = new Vector2(speed,GetComponent<Rigidbody2D>().velocity.x); 
     //GetComponent<Rigidbody2D>().velocity = new Vector2(speed,GetComponent<Rigidbody2D>().velocity.y); 
    } 

} 

良いところは、最後にコンパイルすることができたということです。しかし、私はまだ制約がどのように働くのかわかりません(x、y、z軸の動きに抵抗します)。ジャンプはまだキャップストーンを持たず、変数dJumpLimitはすべてのジャンプを止めていないようです!ブーリアンたちが成し遂げようとしていることを解読しようとするのに苦労しました。旧式のコードが何をしようとしていたのか、私が失敗したことを教えてください。それは私をたくさん助けてくれるだろう。助けてくれてありがとう!

+0

マリオが地上にあるとき、プレイヤーはスペースを押すとマリオが接地されているか、単に二重跳びまだしていない場合、canJumpがtrueの場合、ジャンプは真となり、真です。あなたが地上のチェックをコメントアウトしたので、doubleJumpは常にあなたがいつもダブルジャンプできるという意味では間違いです。 –

+0

ありがとう! –

答えて

0

私はあなたに役立ついくつかの追加のコメントと同じコードを追加しました。

基本的な考え方は、一定の条件の下でジャンプできるようにし、その他の特定の条件の下でのダブルジャンプを許可し、

は、論理的にそれについて考えてみることです、マリオはのみジャンプすることができ、彼は地上にあるとき、私たちすべてのフレームのを確認する必要があります。もし彼が接地されていて、プレイヤーがスペースを押すと、彼はジャンプする。

Marioは、すでに空になっている場合のみ(これ以外の場合は通常のジャンプです)、この「現在の」ジャンプでダブルジャンプしていない場合のみ、ダブルジャンプすることができます。接地

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class NewBehaviourScript : MonoBehaviour { 

    public float speed = 6.0f; 
    //public float j 
    Transform groundCheck; // For checking if grounded 
    //private float overlapRadius = 0.2f; // For checking if grounded 
    public LayerMask whatisGround; // A layer mask to distinguish what is considered as ground 
    private bool grounded = false; // True when Mario is touching ground 
    private bool jump = false;  // Flag to check when player intends to jump 
    public float jumpForce = 700f; // How much force we will apply to our jump 
    private bool doubleJump = false; // This becomes true once we have double-jumped 
    public int dJumpLimit = 5; // A limit to prevent too many jumps happening 

    void Start() 
    { 
     // Find the Transform called "groundcheck" 
     groundCheck = transform.Find ("groundcheck"); 
     // Set the PlayerPrefs integer called "doublejumps" to the value of dJumpLimit 
     PlayerPrefs.SetInt ("doublejumps", dJumpLimit); 
    } 

    void Update() 
    { 
     // If Space is being pressed... 
     if (Input.GetKey(KeyCode.Space)) 
     { 
      jump = true; // Set jump bool to true to indicate our intention to jump 
     } 
    //perhaps put A and D here? 
    } 

    void FixedUpdate() 
    { 
     //to check if Mario is on the ground - this is necessary so we can't jump forever 

     //overlap collider replace Overlap Circle??? 
     //overlap point?? 
     //grounded = GetComponent<Rigidbody2D>().OverlapCollision(groundCheck.position, overlapRadius, whatisGround); 

     // If Mario is touching ground... 
     if (grounded) 
      doubleJump = false; // Make sure that as we are grounded we are not allowed to "jump again" 

     if (dJumpLimit < 1) 
      doubleJump = true; 
     // Set a new bool to true if grounded is true OR doubleJump is false 
     bool canJump = (grounded || !doubleJump); 

     // If the player pressed space AND we are allowed to jump... 
     if (jump && canJump) { 
      // Apply existing x velocity to the x direction 
      GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, 0); 
      // Apply our jump force to the y direction 
      GetComponent<Rigidbody2D>().AddForce (new Vector2 (0, jumpForce)); 

     // If doubleJump is false AND we are not grounded... 
     if (!doubleJump && !grounded) { 
      doubleJump = true; // We have double jumped so set to true 
      dJumpLimit--; // Decrement one from dJumpLimit 
     } 
    } 

     jump = false; // Reset the jump bool to false 

     //code that will work with the limits? 
     //GetComponent<Rigidbody2D>().velocity = new Vector2(speed,GetComponent<Rigidbody2D>().velocity.y); 

     //this will make it stack? 
     //GetComponent<Rigidbody2D>().velocity = new Vector2(speed,GetComponent<Rigidbody2D>().velocity.x); 
     //GetComponent<Rigidbody2D>().velocity = new Vector2(speed,GetComponent<Rigidbody2D>().velocity.y); 
    } 
} 
+0

それでは限界を乗り越えることができましたが、一度だけ飛びます。私はDjumplimitsでこの問題を解決する方法を説明できますか?本当にありがとう! –