2017-04-12 67 views
0

查看關於Unity的幫助論壇,我很快發現我所看到的語法確實過時了(同樣的事情在這裏:Unity Doublejump in C#)。Unity2D中的DoubleJumping限制

下面是我在談論的文章: http://answers.unity3d.com/questions/753238/restrict-number-of-double-jumps.html

例如,在虛空中清醒(),在Unity的當前版本我使用的,它說,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是真的跳變爲真。既然你註釋掉了接地檢查,doubleJump將永遠是假的,這意味着你總是可以雙跳。 –

+0

非常感謝! –

回答

0

我已經添加了相同的代碼和一些額外的評論來幫助你。

的基本思想是允許在一定條件下跳,並允許在某些其他條件雙跳,

嘗試邏輯想想,馬里奧可以當他是在地面上,只有跳,所以我們需要檢查每幀。如果他接地,並且玩家按下空間,我們讓他跳。

現在,我們希望馬里奧能夠雙跳,但前提是他已經在空中(否則這只是一次常規跳躍),並且只有在他沒有在這次「當前」跳躍中雙跳時才能夠雙跳。

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解決這個問題嗎?非常感謝! –