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 /
avatar image
0
Question by RothX · Oct 29, 2015 at 01:24 AM · c#movement2d-platformerjump2d-physics

Jump Code Not Working (2D C#)

So I'm making a 2D platform game in which I need my character to jump, but I am having some issues. (I am scripting with C#). I am very new to Unity, so keep that in mind. I haven't seen anyone else with my problem, and I've been searching for a while. (Or maybe I misinterpreted how this function is supposed to work and this is intended) This is the code for jumping in my PlayerControl script:

 if (Input.GetKeyDown (KeyCode.UpArrow)) {
             if (!inAir) {
                 rb.AddForce(Vector2.up * jumpHeight);
             }
         }

To me it seems pretty simple and self-explanatory. rb is my RigidBody2D, and jumpHeight is a float. inAir is a boolean I made to check if he is already in the air or not, and this check works perfectly by my testing. The problem? Well, the jump is really tiny and fast, it just looks like my character glitches up and then back down, and the weird part is that no matter what I set the jumpHeight to, it is always the same. If I set it to 50000, it is still a tiny little glitch jump. I would consider myself a intermediately advanced programmer, but I am completely new to Unity, so if there is a Unity related mistake that could be the problem, but you are not saying because it just seems so stupid and obvious, please say it anyway, it might be the problem.

EDIT: Everyone is asking me if this is in the Update() function and it is. In fact, it is in the FixedUpdate() function.

Comment
Add comment · Show 2
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 spooneystone · Oct 29, 2015 at 05:08 AM 0
Share

Is this in the update function?

avatar image RothX spooneystone · Oct 29, 2015 at 07:14 PM 0
Share

Yes, the FixedUpdate function

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by jhepong · Oct 29, 2015 at 11:26 AM

try adding a type of ForceMode:

 rb.AddForce(Vector2.up * jumpHeight, ForceMode.Acceleration);

you can play with the type of ForceMode here

Comment
Add comment · Show 3 · 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 centaurianmudpig · Oct 29, 2015 at 12:03 PM 0
Share

Also check the mass of your object. Ideally it should be between 0.1 and 10, anything larger and problems will occur. http://docs.unity3d.com/ScriptReference/Rigidbody-mass.html

avatar image RothX centaurianmudpig · Oct 29, 2015 at 07:16 PM 0
Share

I didn't change my mass, it is 1.

avatar image RothX · Oct 29, 2015 at 07:30 PM 0
Share

Hey, thanks for the answer, but this doesn't really work. In fact, it creates a compiler error, saying that it cannot convert a UnityEngine.Force$$anonymous$$ode to UnityEngine.Force$$anonymous$$ode2D, and that it's an invalid argument for the AddForce function. I saw there was something called Force$$anonymous$$ode2D, but it doesn't have an acceleration property. It has a force and an impulse mode. Changing it to force mode doesn't change it at all, so I would assume that is the default mode. Changing it to impulse mode creates the opposite problem, where he jumps as high as possible no matter what.

avatar image
0

Answer by Nikunj-Kareliya · Oct 29, 2015 at 05:44 AM

Hi @RothX,

Make sure your code is inside Update() method and Rigidbody2D component is attached to the player object.

Good practice is to put your physics based code inside FixedUpdate() method.

 void FixedUpdate () {
 
         if(Input.GetKeyDown(KeyCode.Space)) {
                 rb.velocity = Vector3.Up * jumpHeight;
         }
         
 }

I hope, this will help.

Cheers.

Comment
Add comment · Show 1 · 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 RothX · Oct 29, 2015 at 07:16 PM 0
Share

Yep, my jump is already in the FixedUpdate method, and I have a RigidBody2d component. I just didn't show it in my code, because there's bunch of other stuff inside it too.

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

7 People are following this question.

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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Axis Issue 1 Answer

Distribute terrain in zones 3 Answers

How would you make a pulling/sucking/attracting mechanic? 2 Answers

jump button(2d 2018 lts) 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