- Home /
Double Jump
Hello, im really new to unity and i tried to get my 2d object to jump. But after the first jump it wont jump again. Does anybody have a solution tho this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JumpScript: MonoBehaviour
{
public Vector3 jump;
public float jumpForce = 2.0f;
public bool isGrounded;
Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
jump = new Vector3(0.0f, 2.0f, 0.0f);
}
void OnCollisionStay()
{
isGrounded = true;
}
void OnCollisionExit()
{
isGrounded = false;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow) && isGrounded)
{
rb.AddForce(jump * jumpForce, ForceMode2D.Impulse);
isGrounded = false;
}
}
}
Answer by tormentoarmagedoom · Feb 13, 2020 at 05:12 PM
Hello.
You first need to think what "steps and variables will need". Your program should do this:
IF player is grounded, it can jump and grounded bool becomes false. If before is grounded again press space again, it jumps again and a second bool (called for example secondJump) becomes true. Once character touches ground again, isGrounded and secondJump becomes false again.
This is a very very basic issue, you should be able to do this, and if not, go research, try, read, watch tutorials, try again, try try and try.
Bye.
Thank you very much for your reply, i will try my best to get it working :)
It is great to try things yourself, and i suggest you'll try to apply it all by yourself, but if you get stuck, there are plenty of material regarding double jump out there and this video might help you. https://www.youtube.com/watch?v=DEGEEZmfTT0
Thanks, the vid was realy helpful
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Super Ghouls 'n Ghosts style jumps & double jumps? 1 Answer
Not able to jump 1 Answer
Better 2D Sprite Jump in 3D Plane 1 Answer