Home > Software design >  How can I add a double jump to my player controller in unity (C#)
How can I add a double jump to my player controller in unity (C#)

Time:01-29

I am new to the world of programming and game design, I have followed a few tutorials, and now I am making my first own project. I am working with unity and c# and I already have a working player movement script, but I wanna add double jump to it, how can I do that?

I have tried following a few tutorials but none of them seem to work for me.

I have tried to add double jump now, but I am getting a lot of syntax errors, how can I fix that?

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

public class PlayerMovement : MonoBehaviour

{
    private Rigidbody2D rb;
    private BoxCollider2D coll;

    [SerializeField] private float moveSpeed = 7f;
    [SerializeField] private float jumpForce = 14f;

    [SerializeField] private LayerMask jumpableGround;

    // Start is called before the first frame update
    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        coll = GetComponent<BoxCollider2D>();
    }

    // Update is called once per frame
    private void Update()
    {
        float dirX = Input.GetAxisRaw("Horizontal");
        rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);

        if (Input.GetButtonDown("Jump") && IsGrounded())
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        }
    }

    private bool IsGrounded()
    {
        return Physics2D.BoxCast(coll.bounds.center, coll.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
    }

 private bool hasDoubleJumped = false;
        if (Input.GetButtonDown("Jump") && IsGrounded())
        {
            hasDoubleJumped = false
            rb.velocity = new Vector2(RenderBuffer.velocity.x, jumpForce);
        }
    
        else if (Input.GetButtonDown("Jump") && !hasDoubleJumped)
        {
            hasDoubleJumped = true;
            rb.velocity = new Vector2(RenderBuffer.velocity.x, jumpForce);
        }
}

CodePudding user response:

You could create a boolean like bool hasDoubleJumped and add the following code to the Update:

if (Input.GetButtonDown("Jump") && IsGrounded())
{
    hasDoubleJumped = False
    rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
else if (Input.GetButtonDown("Jump") && !hasDoubleJumped)
{
    hasDoubleJumped = True
    rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}

You would need to add the variable at the top, so for example like that:

private Rigidbody2D rb;
private BoxCollider2D coll;

private bool hasDoubleJumped = False;

Some other tipps: The title of your question is not very good. It doesn't really matter that you write your code in Visual Studio (VS). A better title would have been something like How can I add a double jump to my Unity game in C# or something similar.

EDIT

The following code should work, though I can't say with certainty, I didn't test it.

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

public class PlayerMovement : MonoBehaviour

{
    private Rigidbody2D rb;
    private BoxCollider2D coll;

    private bool hasDoubleJumped = False;

    [SerializeField] private float moveSpeed = 7f;
    [SerializeField] private float jumpForce = 14f;

    [SerializeField] private LayerMask jumpableGround;

    // Start is called before the first frame update
    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        coll = GetComponent<BoxCollider2D>();
    }

    // Update is called once per frame
    private void Update()
    {
        float dirX = Input.GetAxisRaw("Horizontal");
        rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);

        if (Input.GetButtonDown("Jump") && IsGrounded())
        {
            hasDoubleJumped = False
            rb.velocity = new Vector2(RenderBuffer.velocity.x, jumpForce);
        }
    
        else if (Input.GetButtonDown("Jump") && !hasDoubleJumped)
        {
            hasDoubleJumped = True;
            rb.velocity = new Vector2(RenderBuffer.velocity.x, jumpForce);
        }
    }

    private bool IsGrounded()
    {
        return Physics2D.BoxCast(coll.bounds.center, coll.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
    }

CodePudding user response:

I have tried to add the code, but I am getting some syntax errors, how can i fix that?

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

public class PlayerMovement : MonoBehaviour

{
    private Rigidbody2D rb;
    private BoxCollider2D coll;

    [SerializeField] private float moveSpeed = 7f;
    [SerializeField] private float jumpForce = 14f;

    [SerializeField] private LayerMask jumpableGround;

    // Start is called before the first frame update
    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        coll = GetComponent<BoxCollider2D>();
    }

    // Update is called once per frame
    private void Update()
    {
        float dirX = Input.GetAxisRaw("Horizontal");
        rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);

        if (Input.GetButtonDown("Jump") && IsGrounded())
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        }
    }

    private bool IsGrounded()
    {
        return Physics2D.BoxCast(coll.bounds.center, coll.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
    }


    private bool hasDoubleJumped = false;

        if (Input.GetButtonDown("Jump") && IsGrounded())
        {
            hasDoubleJumped = false
            rb.velocity = new Vector2(RenderBuffer.velocity.x, jumpForce);
        }


        else if (Input.GetButtonDown("Jump") && !hasDoubleJumped)
        {
            hasDoubleJumped = true;
            rb.velocity = new Vector2(RenderBuffer.velocity.x, jumpForce);
        }
}
  • Related