- Home /
Double Jump Not working
//This is the code idk why but it only jumps once but everything seems correct please help me I'm stuck for days
void Update() {
//move
Movement();
if (IsGrounded())
{
Jump();
canDoubleJump = true;
}
else if (canDoubleJump == true)
{
Jump();
canDoubleJump = false;
}
void Movement()
{
if (Input.GetKey(KeyCode.D))
rb.velocity = new Vector2(moveForce, rb.velocity.y);
else if (Input.GetKey(KeyCode.A))
rb.velocity = new Vector2(-moveForce, rb.velocity.y);
else
rb.velocity = new Vector2(0, rb.velocity.y);
}
void Jump()
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
}
}
bool IsGrounded()
{
Vector2 position = transform.position;
Vector2 direction = Vector2.down;
float distance = 1.0f;
RaycastHit2D hit = Physics2D.Raycast(position, direction, distance, ground);
if (hit.collider != null)
{
return true;
}
return false;
}
Answer by armandas2005 · Aug 26, 2021 at 05:34 PM
try to add a int which tells your exactly jumpscore like public int jumpsDone
and a int wich tells you how much jumps you can do like public int jumpsLeft
.
than do this in your grounded function
if (IsGrounded() || jumpsDone < jumpsLeft)
{
Jump()
}
if(jumpsDone >= jumpsLeft){
if(IsGrounded()){
jumpsDone = 0;}
in your Jump function write this
void Jump()
{
if (Input.GetKeyDown(KeyCode.Space))
{
jumpsDone++;
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
}
if u have any issues tell me pls
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class player_controller : MonoBehaviour { Rigidbody2D rb; BoxCollider2D bc;
[SerializeField] float moveForce;
[SerializeField] float jumpForce = 10;
public LayerMask ground;
public int jumpHolder;
bool canDoubleJump;
public BoxCollider2D boxCollider2d;
public int jumpsDone = 0;
public int jumpsLeft = 2;
void Start()
{
rb = GetComponent<Rigidbody2D>();
bc = GetComponent<BoxCollider2D>();
}
void Update()
{
void Movement()
{
if (Input.GetKey(KeyCode.D))
rb.velocity = new Vector2(moveForce, rb.velocity.y);
else if (Input.GetKey(KeyCode.A))
rb.velocity = new Vector2(-moveForce, rb.velocity.y);
else
rb.velocity = new Vector2(0, rb.velocity.y);
}
void Jump()
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
}
//move
Movement();
//jump
if (IsGrounded() || jumpsDone < jumpsLeft)
{
Jump();
}
if (jumpsDone >= jumpsLeft)
{
if (IsGrounded())
{
jumpsDone = 0;
}
}
}
bool IsGrounded()
{
Vector2 position = transform.position;
Vector2 direction = Vector2.down;
float distance = 1.0f;
RaycastHit2D hit = Physics2D.Raycast(position, direction, distance, ground);
if (hit.collider != null)
{
return true;
}
return false;
}
void FixedUpdate()
{
}
}
so firstly thank u for helping me in the first place. so I did everything and I "jumpsLeft = 2" & JumpsDone = 0" when declared. still doesn't work
so firstly thank u for helping me in the first place. so I did everything and I "jumpsLeft = 2" & JumpsDone = 0" when declared. still doesn't work
i figured out the issue. Your groundcheck function didnt detected the ground so i fixed it. Let me know if this works :D using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Walking : MonoBehaviour
{
[SerializeField] float moveForce;
[SerializeField] float jumpForce = 10;
public float groundDistance = 0.4f;
public Rigidbody2D rb;
public LayerMask ground;
bool canDoubleJump;
public BoxCollider2D boxCollider2d;
public int jumpHolder;
public int jumpsDone = 0;
public int jumpsLeft = 2;
public Transform groundCheck;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
void Movement()
{
if (Input.GetKey(KeyCode.D))
rb.velocity = new Vector2(moveForce, rb.velocity.y);
else if (Input.GetKey(KeyCode.A))
rb.velocity = new Vector2(-moveForce, rb.velocity.y);
else
rb.velocity = new Vector2(0, rb.velocity.y);
}
void Jump()
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
jumpsDone++;
}
}
//move
Movement();
//jump
if (IsGrounded() || jumpsDone < jumpsLeft)
{
Jump();
}
if (jumpsDone >= jumpsLeft && IsGrounded())
{
jumpsDone = 0;
}
}
public bool IsGrounded()
{
float extraHeightText = .01f;
RaycastHit2D raycastHit = Physics2D.Raycast(boxCollider2d.bounds.center, Vector2.down, boxCollider2d.bounds.extents.y + extraHeightText, ground);
if(raycastHit.collider != null)
{
return true;
}
else
{
return false;
}
}
void FixedUpdate()
{
}
}
Answer by opritaoctav · Aug 26, 2021 at 07:06 PM
doesn't work. i cant even jump, what do I need to assign to Ground Check. (I assigned the tile map still doesn't work)
you dont have to asign anything to GroundCheck i just forgot to delete this. Did you added the ground layer to your ground object? did you attached this script to your player? Add this to the if statement of the IsGrounded function
Debug.Log("is Grounded");
Tell me if this debug works
Your answer
Follow this Question
Related Questions
Limit Jump/Fly Height? (2D) 4 Answers
Triple jumping instead of double 2 Answers
How do I make a character Lunge? 2 Answers