- Home /
The question is answered, right answer was accepted
Double-jump mechanic has an initially high jump
I have a student working on a double-jump mechanic on a 3rd-person perspective. It's a simple script, but with one bug we can't figure out. The double-jump mechanic works fine after the first jump, but that first jump is really high. What is off with our logic that makes that first jump go too far? Any feedback would be most welcome!
using UnityEngine;
using System.Collections;
public class jump : MonoBehaviour {
public float jumpHeight = 0.5F;
bool moveV;
bool isGrounded;
bool doubleJump;
void Start ()
{
isGrounded = true;
doubleJump = false;
}
void FixedUpdate ()
{
moveV = Input.GetButtonDown("Jump");
if (moveV && isGrounded && !doubleJump)
{
rigidbody.AddForce(Vector3.up * jumpHeight);
isGrounded = false;
doubleJump = true;
}
else if (moveV && doubleJump && !isGrounded)
{
rigidbody.AddForce(Vector3.up * jumpHeight);
doubleJump = false;
}
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "ground")
{
isGrounded = true;
doubleJump = false;
}
}
}
Try making his initial jump velocity as Vector3.up * jumpHeight * Time.deltaTime.
I was thinking maybe its not too high just seems that way cause of the frame skip. Try to slow it down to be frame rate independent.
Answer by Alex_May · Feb 01, 2015 at 11:27 PM
I think what's happening is that Input is being updated in frame time, so you're getting double jump code running on the first jump. So Input.GetButtonDown("Jump") is true for more than one consecutive FixedUpdate(). You could debounce the jump button, perhaps by testing it in Update(), setting an internal bool, and then unsetting that bool when you process it in FixedUpdate().
Both solutions solved the problem of the first jump, but now the double jump isn't working. $$anonymous$$aybe I've set the boolean in the wrong place? BTW, thank both of you for the quick responses :-)
void Update()
{
moveV = Input.GetButtonDown("Jump");
hasJumped = true;
}
void FixedUpdate ()
{
if (hasJumped)
{
if (moveV && isGrounded && !doubleJump)
{
rigidbody.AddForce(Vector3.up * jumpHeight);
isGrounded = false;
doubleJump = true;
hasJumped = false;
}
else if (moveV && doubleJump && !isGrounded)
{
rigidbody.AddForce(Vector3.up * jumpHeight * Time.deltaTime);
doubleJump = false;
}
}
}
lightning quick edit, can't guarantee this will work.
void Update()
{
hasJumped = Input.GetButtonDown("Jump");
}
void FixedUpdate ()
{
if (hasJumped)
{
hasJumped = false; // debounce the jump button so the next fixed update won't think we've jumped
if (isGrounded)
{
rigidbody.AddForce(Vector3.up * jumpHeight);
isGrounded = false;
doubleJump = true;
}
else if (doubleJump)
{
rigidbody.AddForce(Vector3.up * jumpHeight);
doubleJump = false;
}
}
// elsewhere...
// if on the ground
// then set isGrounded = true and doubleJump = false
// /elsewhere
}
So simple, and yet it worked! Thank you, sir!
Interesting to see the relationship between Update and FixedUpdate and the results when things go wrong.
you're welcome :) please consider marking the answer as accepted!
Follow this Question
Related Questions
Apply physics material to a third person character? 0 Answers
How to make correct double jump with collor changing? 0 Answers
2D 360 degress platformer example needed 0 Answers
Double Jump Physics 1 Answer
[Physics]Forklift Problem. 1 Answer