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 tokeleth_unity · Jan 20, 2021 at 04:35 PM · scripting problem2d gamejumping

Why is my 2d jumping script weird?

I've written the following code to make a ridigbody2D character jump:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerJump : MonoBehaviour
 {
     Rigidbody2D rb2d;
     private float jumpHeight = 10;
 
     public const string UP = "up";
 
     string jumpState;
 
     // Start is called before the first frame update
     void Start()
     {
         rb2d = GetComponent<Rigidbody2D>();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKey(KeyCode.Space))
         {
             jumpState = UP;
         }
 
         else
         {
             jumpState = null;
         }
     }
 
     private void FixedUpdate()
     {
         if (jumpState == UP)
         {
             rb2d.AddForce(new Vector2(0, jumpHeight), ForceMode2D.Impulse);
         }
     }
 }
 

Now, it kind of works? Like, the character jumps when I press space but only sometimes. Also, if I move in air the character does a weird Mary Poppins glide and loses a lot of vertical speed. What's gone wrong?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Llama_w_2Ls · Jan 20, 2021 at 06:47 PM

Firstly, Update() and FixedUpdate() run on their own timeloops, which means that Update could be called several times before FixedUpdate is called. This means, if you tap space (not hold), there is a slight delay before it actually jumps, which is enough time for the update to run again and set jumpState to null, since space isn't being held anymore. Preferably, you should put everything related to jumping in the same method. Either in FixedUpdate or just Update. Not both please.


Secondly, I see no ground checking there, so your player is able to jump whilst still in the air. This might create some weird gliding issue, as force is still being added, reducing the vertical velocity downward. @tokeleth_unity

Comment
Add comment · Show 8 · 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 tokeleth_unity · Jan 20, 2021 at 07:14 PM 0
Share

Thanks, I know that it's missing a ground check but I wanted to make sure the basic function actually worked first. I'll try putting them in the same update, and writing a ground check.

avatar image tokeleth_unity · Jan 20, 2021 at 07:17 PM 0
Share

Okay so, the problem persists even when everything in Update is moved to FixedUpdate

avatar image Llama_w_2Ls tokeleth_unity · Jan 20, 2021 at 09:42 PM 0
Share

Is the string JumpState needed in order to manage jumping? There should be no logic issues if you just write:

      void FixedUpdate()
      {
          if (Input.GetKey(KeyCode.Space))
          {
              rb2d.AddForce(new Vector2(0, jumpHeight), Force$$anonymous$$ode2D.Impulse);
          }
      }

@tokeleth_unity

avatar image tokeleth_unity Llama_w_2Ls · Jan 20, 2021 at 09:44 PM 0
Share

Probably not honestly, it's a holdover from an earlier version, let me try that out

Show more comments
avatar image Hellium · Jan 20, 2021 at 11:14 PM 1
Share

Preferably, you should put everything related to jumping in the same method. Either in FixedUpdate or just Update. Not both please.

Wrong. Input detection should be done in Update, and applying force in FixedUpdate.


  void Update()
  {
      if (Input.GetKey(KeyCode.Space))
      {
          jumpState = UP;
      }
  }
 
  private void FixedUpdate()
  {
      if (jumpState == UP)
      {
          rb2d.AddForce(new Vector2(0, jumpHeight), Force$$anonymous$$ode2D.Impulse);
          jumpState = null;
      }
  }

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

235 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 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

jumping issues 0 Answers

How do i make my player jump from an angle? (Wallrun Jump) 1 Answer

How can i normalize 2d Vectors? 2 Answers

Unity 2D Combat Text Issue With Multiple Enemies 2 Answers

How can I flip only my 'Player' gameobject, and l leave it's child object alone? 2 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