- Home /
Double Jumping in C#
Hey, I know this is posted a lot, but I'm having a problem with my double-jump script. Is anyone able to help?
As it stands, the character sprite simply moves vertically without having an actual double jump.
Note: there are a lot of variables because I intend to allow quad-jumping eventually.
Thanks in advance!
public int maxjumps = 2;
int jumps;
public float movespeed = 2.0f;
private Rigidbody2D Player_Collider;
public bool jump1;
public bool jump2 = true;
public bool jump3 = true;
public bool jump4 = true;
void Awake()
{
jumps = maxjumps;
}
void Update ()
{
if(Input.GetKey(KeyCode.D))
{
transform.Translate (new Vector3 (1, 0) * movespeed * Time.deltaTime);
}
if(Input.GetKey(KeyCode.A))
{
transform.Translate (new Vector3 (-1, 0) * movespeed * Time.deltaTime);
}
if(Input.GetKey(KeyCode.Space) || Input.GetKey(KeyCode.W))
{
Jump ();
}
}
void Jump()
{
if (jumps == 0)
{
return;
}
if (jump1 == false)
{
GoUp ();
jump1 = true;
jump2 = false;
}
if (jump2 == false)
{
GoUp ();
jump2 = true;
jump3 = false;
}
if (jump3 == false)
{
GoUp ();
jump3 = true;
jump4 = false;
}
if (jump4 == false)
{
GoUp ();
jump4 = true;
}
}
void GoUp()
{
GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, 5), ForceMode2D.Impulse);
jumps = jumps - 1;
}
void OnCollisionEnter2D(Collision2D collide)
{
if(collide.gameObject.tag == "Ground")
{
jump1 = false;
jump2 = true;
jump3 = true;
jump4 = true;
}
}
}
There's a few problems here.
Firstly the way you have arranged your 'if's, if you go into the jump1 if, you set jump2 to false and that makes you enter the next if... that goes for all the jumpX ifs so you always enter all of them or none of them.
Secondly I don't see a reason to use those jump1,2,3,4 variables at all. You already have the 'jumps' and 'maxJumps' variables that holds all the info you need about jumping: how many you have done and how many you can do. You can jump if 'jumps > 0' and when you land you reset jumps to 'maxJumps'.
Answer by $$anonymous$$ · Jul 07, 2016 at 05:53 PM
@NoseKills I want to thank you and apologize. I'd figured out how to code it correctly (which will be below for any who need help with this), but I couldn't delete the question. So, thank you for your help, and I'm sorry for the trouble.
Here's the new code:
public int maxjumps = 2;
int jumps;
float jumpforce = 5f;
void Update()
{
if(Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.W))
{
Jump ();
}
}
void Jump()
{
if (jumps > 0)
{
gameObject.GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, jumpforce), ForceMode2D.Impulse);
grounded = false;
jumps = jumps - 1;
}
if (jumps == 0)
{
return;
}
}
void OnCollisionEnter2D(Collision2D collide)
{
if(collide.gameObject.tag == "Ground")
{
jumps = maxjumps;
grounded = true;
movespeed = 2.0F;
Answer by Petch12 · Jul 07, 2016 at 05:53 PM
By quad I presume you mean 4 jumps (?) It doesn't really matter though how many times you want to jump. I would set 2 integers for the jumps (both public):
public int maxAmountOfJumps = 2;
public int Jumped = 0;
void Awake()
{
//Not sure why you need this to be honest.
}
void Update()
{
if (Input.GetKey(KeyCode.Space) || Input.GetKey(KeyCode.W))
{
Jump();
}
}
//Now add Jump function
void Jump()
{
if (Jumped <= maxAmountOfJumps)
{
GoUp();
}
else
{
return;
}
}
void GoUp()
{
//Insert Jump Code.
Jumped +=1;
}
void OnCollisionEnter2D(Collision2D collide)
{
if (collide.gameObject.tag == "Ground")
{
Jumped = 0;
}
}
So every time you land you reset Jumped to 0 that way you can only jump 2 times and no more.
Hopefully if this doesn't fix your problem it will at least give you some ideas you can work with.
EDIT: Sorry by the way I made this script in visual studio not via Unity so copying and pasting might not work very well.
I can't seem to jump anymore after the first double jump dan land on the ground.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
Double jump 2D Platformer 1 Answer