简体   繁体   English

Unity2D中的双跳限制

[英]DoubleJumping Limits in Unity2D

Looking at help forums on Unity, I figured out quickly that the syntax I was looking at was really outdated (Same thing on here: Unity Doublejump in C# ). 在Unity的帮助论坛上,我很快发现我正在查看的语法确实过时了(在这里同样的东西: C#中的Unity Doublejump )。

Here's the article I was talking about: http://answers.unity3d.com/questions/753238/restrict-number-of-double-jumps.html 这是我正在谈论的文章: http : //answers.unity3d.com/questions/753238/restrict-number-of-double-jumps.html

For instance, in the void Awake(), in the current version of Unity I'm using, it says that rigidbody2D.fixedAngle = true; 例如,在空的Awake()中,在我正在使用的当前版本的Unity中,它说的是elasticbody2D.fixedAngle = true;。 was no longer supported, and I needed to use constraints on the gameObject I was trying to program (what axis should I use... the x,y or z?). 不再受支持,并且我需要在尝试编程的gameObject上使用约束(我应使用哪个轴... x,y或z?)。 After doing some editing and after looking up the error messages, I was able to change all of the rigidbody2D.velocity into the updated syntax, which was GetComponent ().velocity . 在进行了一些编辑之后,并在查找了错误消息之后,我能够将所有的bodybody2D.velocity更改为更新的语法,即GetComponent().velocity。

Here's my code: 这是我的代码:

    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);
    }

}

Good thing is that it was able to compile in the end. 好东西是它最终能够编译。 But I still don't know how the constraints work (they resist motion on the x,y,z axis right?). 但是我仍然不知道约束是如何工作的(它们抵抗x,y,z轴上的运动对吗?)。 The jumps still don't have a capstone, and the variable dJumpLimit doesn't seem to stop all the jumping! 跳跃仍然没有顶点,并且变量dJumpLimit似乎并没有阻止所有跳跃! I had also lot of trouble trying to decipher what the booleans were trying to accomplish, and it would help a lot if you tell me what the outdated code was trying to do, and what I failed doing so. 尝试破译布尔函数要完成的工作也很麻烦,如果您告诉我过时的代码要做什么,而我却失败了,这将大有帮助。 That would help me a lot.Thanks so much for helping!!! 这将对我有很大帮助。非常感谢您的帮助!!!

I have added the same code with some additional comments to help you. 我在同一代码中添加了一些其他注释来帮助您。

The basic idea is to allow jumping under certain conditions, and allow double jumping under certain other conditions, 基本思想是在某些条件下允许跳跃,在某些其他条件下允许双跳,

Try to think about it logically, Mario can only jump when he is on the ground, so we need to check that every frame . 尝试从逻辑上考虑一下,马里奥只有在地面上才能跳起来,因此我们需要检查每一帧 If he is grounded, and the player presses space, we make him jump. 如果他被搁浅,并且玩家按了空格,我们会让他跳。

Now, we want Mario to be able to double-jump but only if he is already in the air (otherwise it's just a regular jump) and only if he has not already double-jumped in this "current" jump. 现在,我们希望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