Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Captain_Dando · Nov 19, 2013 at 07:51 AM · 2d gamerigidbody2daddforce

Character instantly jumps instead of smoothly gaining altitude

Hi everyone, I'm working on a 2D sidescroller using the new 2D features from 4.3. To start off, I created a player movement script that uses the 4.3 workflow demo as a base. I've copied their jump scripting to a tee, but for some reason, my character's jump works unusually. Instead of rising gradually, he shoots up, and then descends normally (as seen below). The rigidbody2D settings are exactly the same as well.

Here is the script I'm using to jump:

 bool jump;
 void Update(){
    grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
    if (Input.GetButtonDown("Jump") && grounded){
       jump = true;
    }
 }
 
 void FixedUpdate(){
    if (jump){
       rigidbody2D.AddForce(new Vector2(0f, 1000));
       jump = false;
    }
 }

alt text

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 hweemiin · May 22, 2015 at 07:11 AM 0
Share

I am having the same issue even with the 'Apply Root $$anonymous$$otion' already turned off...

avatar image java22 · Mar 26, 2016 at 12:20 PM 0
Share

my 2d game has been doing the same thing and i fixed it by adding not script to the character but animation

3 Replies

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

Answer by Captain_Dando · Nov 20, 2013 at 07:38 AM

Ok, I initially closed this question because I thought the answer had nothing to do with the script, which is true, but I've discovered the source of the problem and I'd like to share it for anyone else who's battling with this problem. when I turned the "Apply Root Motion" option off on the animator component my character was able to move smoothly and the problem disappeared. I'm not entirely sure why this was a problem but I think it may have something to do with the fact that the demo player character was made of multiple parts which moved separately as opposed to the single sprite animation which I used. in any case, turn it off and the problem should be resolved

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

Answer by gajdot · Nov 19, 2013 at 08:01 AM

The problem is that you add a huge force to it, which blows him up in the air seemingly teleporting up instantly.

I believe you could fix this by applying a smaller value over time, so it would transition a bit more nicely.

So basically add a smaller force to it and don't set the jump to false instantly, instead make a timer and set it to false only a second later.

You can do this two ways co-routine or just add a Time variable and when jump is true add deltaTime to it. When this value gets higher then the threshold and jump is true then set jump to false and zero down the time variable.

Comment
Add comment · Show 5 · 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 Captain_Dando · Nov 19, 2013 at 08:09 AM 0
Share

I've thought of doing this, but I'm concerned that it will create bugs if the jump coroutine gets interrupted. also, I'm not sure why, but in the 2d example project the jump works perfectly, even at 1000 as well, yet with $$anonymous$$e as you can see it's instant. setting the velocity of the character along the y axis to any value does the same thing as well

avatar image gajdot · Nov 19, 2013 at 08:21 AM 1
Share

To be honest I didn't checked the example, so I don't know how does it work there, but I'll see into that. $$anonymous$$eanwhile you could try the way I suggested without coroutine with the Time variable update in the fixedUpdate.

Check what's your mass of your character, in the example project it's only 1. If you have higher mass that can make the difference. If not, I don't know what's the difference, but the timer can solve your problem.

avatar image Captain_Dando · Nov 19, 2013 at 11:01 AM 0
Share

Ok, I've replicated my initial code in a completely new project and strangely enough the problem is resolved. I'm not sure what caused it, but it's nothing in the script, so I'm going to close the question

avatar image gajdot · Nov 19, 2013 at 11:05 AM 1
Share

Ok, at least you got rid of your problem.

avatar image Captain_Dando · Nov 19, 2013 at 11:08 AM 0
Share

Thanks for your answer in any case

avatar image
0

Answer by Lazzle · Nov 14, 2018 at 10:23 PM

Just came across this problem myself, so just incase anyone else comes across the same problem: in my case the problem was that i was resetting the Y velocity in my movement controls


RB.velocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical") ;


so the upward velocity is getting zero'd in the next frame and the character starts falling (the characters downward velocity is also zero'd each frame, hence why he falls at a constant rate, rather than accelerating like normal)


Fix: RB.velocity = new Vector3(Input.GetAxis("Horizontal") , RB.velocity.y, Input.GetAxis("Vertical"));


My assumption is that in the case above some other script that also acts on the character is doing something similar (resetting the velocity every frame)

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

22 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

Related Questions

How to calculate the rb.AddForce for my game? 1 Answer

rigidbody2d.addforce for is not working for the x axis 0 Answers

Add Force if GameObject is detected in a Polygon Collider 2D 1 Answer

add force on topdown bullet 1 Answer

Adding force to mouse position adds force relative to screen center 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