Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Gusic · May 14, 2014 at 08:04 PM · c#2djump

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Shmulzious · May 14, 2014 at 08:26 PM 1
Share
  1. 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.

  2. 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()

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Wiki

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;
         }
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image WhiteIce2112 · Jan 31, 2016 at 08:36 PM 0
Share

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.

avatar image KAYUMIY · Mar 30, 2016 at 07:53 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Super Ghouls 'n Ghosts style jumps & double jumps? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges