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;
}
}
}
@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;
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.