Hi! I am new to unity coding and i am developing my first 2D game. But I am stuck on how to make my character double jump. I am currently on Unity 5. Can someone explain to me how it could be done with my current jumping script?
Script:
using UnityEngine;
using System.Collections;
public class Jump : MonoBehaviour {
public float jumpHeight;
private bool isJumping = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//Jumping Section
if (Input.GetKeyDown (KeyCode.Space))
{
if (isJumping == false)
{
gameObject.GetComponent<Rigidbody2D> ().AddForce (Vector2.up * 500);
isJumping = true;
}
}
}
//this bit is to check if the object that collided with it has the ground tag
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Ground")
{
isJumping = false;
}
}
}
One way you can do this is to set a variable to keep track of if they have second jumped yet.
public float jumpHeight;
private bool isJumping = false;
private bool isJumping2 = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//Jumping Section
if (Input.GetKeyDown (KeyCode.Space))
{
if (isJumping == false)
{
gameObject.GetComponent<Rigidbody2D> ().AddForce (Vector2.up * 500);
isJumping = true;
}
else if (!isJumping2)
{
AddMoreForce & isJumping2 = true;
}
}
}
//this bit is to check if the object that collided with it has the ground tag
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Ground")
{
isJumping = false;
isJumping2 = false;
}
}
hi i recently made my own jump function and i try adding in a double jump function but it seems like it
wont jump only twice it will do infinite jump in the air how would i only make it jump twice
heres the code
using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour {
public bool jump = false; // if it jumps
public bool doublejump; // if it jumps
public float jumpForce = 0.2f; // how high the player jump
public float doublejumpForce = 0.2f; // how high the player jump
private Transform groundCheck; // if the player is on ground
private bool grounded = false; // weather the player is on ground or not
void Start ()
{
// finding a ground layer called "groundCheck"
groundCheck = transform.Find("groundCheck");
}
void Update()
{
// if linecast hits a groundcheck then the player is on the ground
grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
if (Input.GetButtonDown("Jump") && grounded)
jump = true;
if (Input.GetButtonDown("Jump") && !grounded && !doublejump)
jump = true;
}
void FixedUpdate()
{
// if the player will jump
if (jump)
{
// add vertical force to the player
GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, jumpForce));
// when the player has just land on the ground it needs to wait before it can jump again
jump = false;
}
else if (doublejump)
{
// add vertical force to the player
GetComponent<Rigidbody2D>().velocity.y(new Vector2(0f, jumpForce));
// when the player has just land on the ground it needs to wait before it can jump again
jump = false;
}
}