Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Solvaij · Sep 28, 2019 at 10:57 AM · 2d-platformerjumpingframeratebuilds

2D Jump Height Inconsistiency and Framerate Independence

I am relatively new to Unity, and I'm trying to build a 2D platformer. I have a working player controller, but I've noticed that the jump height is sometimes different between the game running in the editor and the actual build. I think this probably has to do with the fact that everything is happening in Update instead of FixedUpdate, and I feel like it might be related to framerate independence? I know that in order to get framerate independence I need to multiply something by Time.deltaTime, but every time I try this in this script, it makes the character unable to move/jump. I think I just need a little clarification on how FixedUpdate/Time.deltaTime work and thoughts on why this inconsistency might be happening.

Below is the working code. Thank you!!

 public float maxSpeed;               //currently set to 10
 public float jumpMax;                //currently set to 20  <- this is max velocity
 public float initialForce;           //currently set to 650
 public float acceleration;           //currently set to 100
 public LayerMask groundLayer;
 
 private bool grounded;
 private bool peaked;                 // true if the player has reached the max jump height
 private int jumped = 0;              // number of times jumped, for future use in double jumping
 private float lookDirection = 1;     // used in animation
     
 private Animator animator;
 private Rigidbody2D rigidbody;
     
 void Awake()
 {
             rigidbody = GetComponent<Rigidbody2D>();
             animator = GetComponent<Animator>();
 }
     
 void Update()
 {
             float move = Input.GetAxis("Horizontal");
             if (!Mathf.Approximately(move, 0.0f))
             {
                 lookDirection = move;
                 lookDirection = (lookDirection / Mathf.Abs(lookDirection));
             }
             animator.SetFloat("Look", lookDirection);
             animator.SetFloat("Speed", Mathf.Abs(rigidbody.velocity.x));
             rigidbody.velocity = new Vector2(move * maxSpeed, rigidbody.velocity.y);
        
       
             if (Input.GetButtonDown("Jump"))                //initializes the jump
             {
                 if (grounded)
                 {
                     rigidbody.AddForce(Vector2.up * initialForce);
                 }
             }
             if (Input.GetButton("Jump"))                    //allows variable jump heights
             {
                 if (rigidbody.velocity.y < jumpMax && !peaked)
                 {
                     if (rigidbody.velocity.y > 0.0001)      //prevents continuous jumping
                         rigidbody.AddForce(Vector2.up * acceleration);
                 }
                 if (rigidbody.velocity.y > jumpMax)
                 {
                     peaked = true;
                 }
             }
     
     
             if (rigidbody.velocity.y > 0.0001)
             {
                 animator.SetBool("Ascend", true);
                 animator.SetBool("Descend", false);
             }
             else if (rigidbody.velocity.y < -0.0001)
             {
                 animator.SetBool("Descend", true);
                 animator.SetBool("Ascend", false);
             }
             else
             {
                 animator.SetBool("Descend", false);
                 animator.SetBool("Ascend", false);
             }
 }
     
 void OnTriggerEnter2D(Collider2D other)
 {
             if (other.gameObject.tag == ("Ground"))
             {
                 jumped = 0;
                 grounded = true;
                 peaked = false;
                 animator.SetBool("Descend", false);
                 animator.SetBool("Ascend", false);
                 animator.SetTrigger("Grounded");
             }
 }
 void OnTriggerExit2D(Collider2D other)
 {
             if (other.gameObject.tag == ("Ground"))
             {
                 jumped = 1;
                 grounded = false;
             }
 }
Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

191 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Jump from the inclined surfaces 0 Answers

2D Side Scroller Help. I have a picture to show you! 0 Answers

I can't do jump in my 2D game 1 Answer

Varible jump heights 2 Answers

My game seems to be running too fast for certain tasks 0 Answers


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