- Home /
im trying to make a double jump but its combining the jumps into one at once and idk how to fix it
im trying to make a double jump its not letting me click the second time and instead makes my player go flying with double the force
public float MoveSpeed = 3;
private Rigidbody PlayerRB;
public float JumpForce = 10;
public float GravMod = 2;
private float JumpDelay = 0.07f;
private float TimeStamp;
public bool IsOnGround = false;
public bool HasDoubleJump;
public bool AllowedDoubleJump;
// Start is called before the first frame update
void Start()
{
PlayerRB = GetComponent<Rigidbody>();
Physics.gravity *= GravMod;
}
// Update is called once per frame
void Update()
{
//Basic movement using wasd
if (Input.GetKey(KeyCode.A))
{
transform.Translate(MoveSpeed * Time.deltaTime * Vector3.left);
}
if (Input.GetKey(KeyCode.D))
{
transform.Translate(MoveSpeed * Time.deltaTime * Vector3.right);
}
if ((IsOnGround == true) && Time.time >= TimeStamp && Input.GetKey(KeyCode.Space))
{
//Jump code
if (HasDoubleJump == true && AllowedDoubleJump == true)
{
PlayerRB.AddForce(Vector3.up * JumpForce, ForceMode.Impulse);
AllowedDoubleJump = false;
TimeStamp = Time.time + JumpDelay;
if (Input.GetKey(KeyCode.Space))
{
PlayerRB.AddForce(Vector3.up * JumpForce, ForceMode.Impulse);
Debug.Log("Second Jump");
}
}
else if (HasDoubleJump == false)
{
PlayerRB.AddForce(Vector3.up * JumpForce, ForceMode.Impulse);
IsOnGround = false;
TimeStamp = Time.time + JumpDelay;
}
}
}
private void OnCollisionEnter(Collision col)
{
if (col.gameObject.CompareTag("Ground"))
{
IsOnGround = true;
TimeStamp = Time.time + JumpDelay;
AllowedDoubleJump = true;
}
}
}
Answer by Waterpolo7 · Dec 23, 2021 at 09:53 PM
Could you try changing all the Input.GetKey(KeyCode.Space) to Input.GetKeyDown(KeyCode.Space). Now the game registers a space for every frame you're holding space (a frame is very short, so a small tap could already be causing the ultra jump) and that will be changed when he only registers the button coming down.
I hope this works, let me know :)
Your answer
Follow this Question
Related Questions
Double Jump using First Person Controller 1 Answer
Double jump does not work why? 0 Answers
How can I edit this script to double jump? 0 Answers
Counter for double jump don't work 1 Answer
(Solved) Double jumping 3 Answers