Question by
miki98kill · Feb 14, 2016 at 11:22 AM ·
double jump
Double Jump problem [C#]
Hi everyone! I am trying to do double jumping with this script : using UnityEngine; using System.Collections;
public class movement : MonoBehaviour {
public float velocità;
public Rigidbody2D rb;
public float jumpHeight;
public int jumpcount = 0;
public bool grounded=false;
// Use this for initialization
void Start () {
}
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "Base")
{
grounded = true;
}
}
// Update is called once per frame
void Update () {
rb = GetComponent<Rigidbody2D>();
if (Input.GetKey("a"))
{
rb.velocity = new Vector2(-velocità,rb.velocity.y);
}
if(Input.GetKey("d"))
{
rb.velocity = new Vector2(velocità, rb.velocity.y);
}
if (Input.GetKeyDown("w"))
{
grounded = false;
rb.velocity = new Vector2(0, jumpHeight);
if(jumpcount>2 && !grounded)
{
rb.velocity = new Vector2(0, 0);
jumpcount = 0;
}
jumpcount = jumpcount + 1;
}
}
}
The groundcheck works fine but i don't understand why if my player is not grounded is able to jump! Sorry for my english and thank in advance.
Comment
Answer by miki98kill · Mar 07, 2016 at 05:32 PM
Anyone can help me ? Now my code is this: using UnityEngine; using System.Collections;
public class movement : MonoBehaviour {
public float velocità;
public Rigidbody2D rb;
public float jumpHeight;
public int jumpcount = 0;
public bool grounded=false;
public bool CanDobuleJump=false;
// Use this for initialization
void Start () {
}
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "Base")
{
grounded = true;
}
}
// Update is called once per frame
void Update () {
rb = GetComponent<Rigidbody2D>();
if (Input.GetKey("a"))
{
rb.velocity = new Vector2(-velocità,rb.velocity.y);
}
if(Input.GetKey("d"))
{
rb.velocity = new Vector2(velocità, rb.velocity.y);
}
if (Input.GetKey("w"))
{
if (jumpcount > 1 && !grounded && CanDobuleJump)
{
CanDobuleJump= true;
rb.velocity = new Vector2(0, 0);
grounded = true;
jumpcount = 0;
Debug.Log(+jumpcount);
}
else
{
grounded = false;
rb.velocity = new Vector2(0, jumpHeight);
jumpcount = jumpcount + 1;
CanDobuleJump = true;
Debug.Log(+jumpcount);
}
}
}
}
Answer by tadadosi · May 26, 2020 at 04:44 PM
This should work as you intended (Added comments to explain some things):
using UnityEngine;
public class DoubleJump : MonoBehaviour
{
public Rigidbody2D rb;
public float moveSpeed; // use a better variable name like this one
public float jumpHeight;
public int jumpMaxAmount = 2; // change this value to get more jumps while in mid-air
public int jumpCount = 0;
public bool grounded = false;
private void Awake()
{
// Store this only one time in Awake or Start methods
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetKey("a"))
{
rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
}
if (Input.GetKey("d"))
{
rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
}
if (Input.GetKeyDown("w"))
{
if (grounded) // if grounded player can jump
{
grounded = false;
Jump(); // made it into a method if you wanna somehow change that method later
jumpCount = 1; // make it 1 to account for the first jump
}
else if (jumpCount < jumpMaxAmount && !grounded) // if mid-air and hasn't reached MaxAmount
{
Jump();
jumpCount++; // Add +1 for any extra jump
}
}
}
// New method for jumping
private void Jump()
{
rb.velocity = new Vector2(0, jumpHeight);
}
void OnCollisionEnter2D(Collision2D coll)
{
// better use CompareTag like this, tag alone is inefficient
if (coll.transform.CompareTag("Base"))
{
grounded = true;
}
}
}