Added Double Jump

While I was working on limiting the infinite jump problem, I thought it would be fun to arrange the platforms in different heights and have a double jump instead, not sure if this is the right way but here is how I did it:

const GRAVITY = 3500
var jumps = 0
const MAX_JUMPS = 2

func jump():
	if Input.is_action_just_pressed("ui_up") and jumps < MAX_JUMPS:
		motion.y = JUMP_SPEED
		jumps += 1
	elif is_on_floor():
		jumps = 0

Nice job. That is almost exactly how I implemented double jump for my charecter as well. Id say that there isn’t really a wront way to do something as long as you get the functionality you are looking for, and it doesn’t create any bugs in your program!

Happy Godoting,
Greg

This is how I did it when I was assigned a challenge not to jump infinitely, in case it would help someone.
Commented parts are about double jumping.

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

public class PlayerMovement : MonoBehaviour
{

Vector2 moveInput;
Rigidbody2D myRigidbody;
[SerializeField] int movementSpeed = 5;
[SerializeField] int jumpSpeed = 5;
bool flipX = false;
Animator myAnimator;
Collider2D myCollider2D;
bool canJump = false; // = true when touching Ground Layer (mask 64)
int jumpsAvailable;  // Number of jumps available




void Start()
{
    myRigidbody = GetComponent<Rigidbody2D>();
    myAnimator = GetComponent<Animator>();
    Debug.Log(LayerMask.GetMask("Ground"));  
    myCollider2D = GetComponent<Collider2D>();


}

void Update()
{
    Run();
    GetComponent<SpriteRenderer>().flipX = flipX;
    canJump = myCollider2D.IsTouchingLayers(64);   // not sure if Rick did differently, just started a challenge
    DoubleJump(); // because we love it in video games!


}


void Run()
{
    Vector2 playerVelocity = new Vector2(moveInput.x * movementSpeed, myRigidbody.velocity.y);
    myRigidbody.velocity = playerVelocity;
}

void DoubleJump()
{
    if (canJump)
    {
        jumpsAvailable = 1;     // jumps are available again after you touch the ground
    }
}

void OnJump(InputValue value)
{
    if (canJump || jumpsAvailable > 0)  //   || means OR
    {
        if (value.isPressed)
        {
            myRigidbody.velocity += new Vector2(0f, jumpSpeed);
            jumpsAvailable--;                                    // you've got only 1 more jump left until you touch the ground again
            if (!canJump) Debug.Log("Double jump!");            // showing in console if we jumped while not touching the ground, can do it once because of our code
            
        }
    }

}
void OnMove(InputValue value)
{
    moveInput = value.Get<Vector2>();
    Debug.Log(moveInput);
    if (moveInput.x < 0)
    {
        flipX = true;
        myAnimator.SetBool("isRunning", true);

    }
    else if (moveInput.x > 0)
    {
        flipX = false;
        myAnimator.SetBool("isRunning", true);
    }
    else
    {
        myAnimator.SetBool("isRunning", false);
    }
}

}

Privacy & Terms