- Home /
Help With Simple Jump Script
Hi guys
i have done most of my script for my jump script. i have made it so when the player is in the air he cant keep jumping until he lands. but the problem is when the player lands he cant jump anymore please help guys.
Thank You
using UnityEngine;
using System.Collections;
public class Jump : MonoBehaviour {
public bool grounded = true;
public float jumpPower = 1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Jump") && grounded == true) {
rigidbody2D.AddForce(transform.up*jumpPower);
grounded = false;
}
}
}
Answer by Maui-M · Feb 17, 2014 at 11:15 PM
You need to set your grounded variable back to true at some point if you want to jump again.
void Update () {
if(!grounded && rigidbody2D.velocity.y == 0) {
grounded = true;
}
if (Input.GetButtonDown("Jump") && grounded == true) {
rigidbody2D.AddForce(transform.up*jumpPower);
grounded = false;
}
}
Won't this script make the rigidbody jump higher once its velocity reaches zero at the top? I am new to Unity so if I am making a mistake somewhere its my bad!!
It allows the player to jump again when he/she is falling back to the ground indeed, but only if the player presses jump button ! (And velocity.y must be at exatcly 0 on a frame, which shouldn't happen that much)
This is a bit of a late response but if you were really concerned about hitting 0 velocity at the top you could always track your previous velocity. if the previous velocity was negative and now it's zero you know you were moving down and are now on the ground.
Your answer
Follow this Question
Related Questions
Does Scripting Jump exist? (How To use/write goto) 3 Answers
C# how to make Character Motor Jump Infinitely Upward 2 Answers
Simple script to follow player only shows the camera background 1 Answer
I need a script in C# which moves an object in only one direction at a time. 1 Answer
Jump and run on a circle (2D) 0 Answers