- Home /
Help With Jump Script (C#)
Hi Guys,
I have a problem with my jump script, i can jump normally. but sometimes my player jumps to high randomly high. is there a problem with my script.
using UnityEngine;
using System.Collections;
public class Jump : MonoBehaviour {
public bool grounded = true;
public float jumpPower = 190;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// Do something
if(!grounded && rigidbody2D.velocity.y == 0) {
grounded = true;
}
if (Input.GetButtonDown("Fire1") && grounded == true) {
rigidbody2D.AddForce(transform.up*jumpPower);
grounded = false;
}
}
}
Thank You
Are you also pressing the button when you are in the air? Because velocity will become 0 also in mid air right before your character starts dropping, turning isGrounded to true when it is not actually grounded.
From my experience (not a ton but i am building my own 2d platformer) the movement should be handled on FixedUpdate() while the input should be handled on Update()
Answer by Kiwasi · May 14, 2014 at 11:04 PM
AddForce only changes the velocity during fixed update. Fixed update never runs faster then 60 FPS. Update on the other hand will run as fast as it can. Even if it only runs at 61 FPS then every now and then it will run twice before FixedUpdate runs. Forces added are queued until FixedUpdate runs.
This is what is most likely happening:
Update() runs. The character is standing still so grounded returns true. A force is added, but the character does not move.
Update() runs. The character is still standing still, so grounded returns true. Another force is added, character still does not move.
FixedUpdate() runs. All queued forces are added to the character and the character jumps twice as high.
Here is a code to fix to try (C#, untested):
using UnityEngine;
using System.Collections;
public class Jump : MonoBehaviour {
public bool grounded = true;
public float jumpPower = 190;
private bool hasJumped = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// Do something
if(!grounded && rigidbody2D.velocity.y == 0) {
grounded = true;
}
if (Input.GetButtonDown("Fire1") && grounded == true) {
hasJumped = true;
}
}
void FixedUpdate (){
if(hasJumped){
rigidbody2D.AddForce(transform.up*jumpPower);
grounded = false;
hasJumped = false;
}
}
}
I'm using this script on a sphere and it works fine until I start moving. If I jump while moving it will either work or just change the direction I'm moving in.
This code is not working. gameObject is still going up ransomly. We need only one jump. Going up and as soon as going down. In this code, gameObject is only going up without stop.
Answer by bTaY225 · Sep 23, 2016 at 11:40 AM
perhaps it's a wee bit too late but if you can get the player object to check the ground object's tag, (OnCollision function) you can turn jump on and off with a bool. if it's colliding with this object with this specific tag you can jump. If not, you can't. check if bool == true && collision tag = "thistag" jump else !jump.
hope this helps someone else out in the future.
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
iTween gradual jump 2D 0 Answers
Distribute terrain in zones 3 Answers
[C#] Platform Effector 2D really glitchy 0 Answers