簡體   English   中英

Unity2D中的雙跳限制

[英]DoubleJumping Limits in Unity2D

在Unity的幫助論壇上,我很快發現我正在查看的語法確實過時了(在這里同樣的東西: C#中的Unity Doublejump )。

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

例如,在空的Awake()中,在我正在使用的當前版本的Unity中,它說的是elasticbody2D.fixedAngle = true;。 不再受支持,並且我需要在嘗試編程的gameObject上使用約束(我應使用哪個軸... x,y或z?)。 在進行了一些編輯之后,並在查找了錯誤消息之后,我能夠將所有的bodybody2D.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似乎並沒有阻止所有跳躍! 嘗試破譯布爾函數要完成的工作也很麻煩,如果您告訴我過時的代碼要做什么,而我卻失敗了,這將大有幫助。 這將對我有很大幫助。非常感謝您的幫助!!!

我在同一代碼中添加了一些其他注釋來幫助您。

基本思想是在某些條件下允許跳躍,在某些其他條件下允許雙跳,

嘗試從邏輯上考慮一下,馬里奧只有在地面上才能跳起來,因此我們需要檢查每一幀 如果他被擱淺,並且玩家按了空格,我們會讓他跳。

現在,我們希望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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM