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 XenosTiger · Nov 04, 2018 at 11:37 PM · c#unity 2dbeginner

Character teleports upwards instead of jumping smoothly Unity2D

I am rather new to Unity, and am attempting to create my first game. Everything has gone as smoothly as expected until now. When attempting to jump, my character instead seemingly teleports to the top of of it's jump before gliding down to the ground. No matter what I have tried and the seemingly endless threads I have searched, I can't find an answer. Any help would be appreciated, thanks. My character script is linked below.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Poro : MonoBehaviour {
 
     public float speed = 10f;
     public float force = 300f;    
 
     Rigidbody2D rb;
 
 
     void Start () {
         rb = GetComponent<Rigidbody2D>();
     }
 
 
     void FixedUpdate () {
         rb.velocity = Vector2.right * speed;
 
         if (Input.GetKeyDown(KeyCode.UpArrow))
         {
             rb.AddForce(Vector2.up * force);  
         }
     }
   
 
 }
 
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

2 Replies

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

Answer by Eno-Khaon · Nov 05, 2018 at 01:27 AM

Here's what's happening during FixedUpdate().

First, you're setting your velocity to have no vertical element.

 rb.velocity = Vector2.right * speed;


Then, you add an arbitrary vertical force.

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


First, let's address the velocity itself. If you don't want to drift downward when directly setting a Rigidbody's velocity, you need to keep your vertical speed intact.
 rb.velocity = new Vector2(speed, rb.velocity.y);


Furthermore, the distance your character is moving up when jumping right now is only a single physics calculation of movement before "floating" back down.


Second, there's the Input. You DON'T want to use Input.GetKeyDown() during FixedUpdate(). Typically, this results in arbitrary inputs being missed. All Input instructions occur during Update(), whereas FixedUpdate() occurs with different timing. If you get two frames between a FixedUpdate(), you'll miss the frame where the key was pressed. To be certain that the physics will be processed accurately, however, the best practice is to apply physics changes during FixedUpdate().
 bool jump = false;

 void Update()
 {
     if(Input.GetKeyDown(KeyCode.UpArrow))
     {
         jump = true;
     }
 }
 
 void FixedUpdate()
 {
     if(jump)
     {
         jump = false;
         rb.AddForce(Vector2.up * rb.mass * force, ForceMode2D.Impulse);
     }
 }


Finally, the force itself. I tweaked it in my example above already, but here's what should change:

Right now, your jump force scales based on the physics update rate of your game. If you decide to turn it up or down, your character will jump lower or higher respectively. AddForce() (2D), by its default, applies force using ForceMode2D.Force. This results in a steady force that's applied evenly per frame, with a goal of applying the designated force per-second. By this logic, if your physics update rate is the default of 50 FixedUpdate() cycles per second, your force of 300 scales down to 6 units-per-second immediate force applied. If you changed your physics rate to, say, 100 per second, then that 300 force scales down to only 3.

ForceMode2D.Impulse, however, applies its force instantaneously. I multiplied the force by the character's mass to counteract that element of the equation. This results in a consistent force applied to the character when jumping, no matter their mass or physics rate.
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 XenosTiger · Nov 05, 2018 at 03:22 AM 0
Share

Hey, thanks for the super in-depth explanation! Not only does it work, I think I know why it works too. Thanks so much!!!

avatar image
0

Answer by Otavio_ · Nov 05, 2018 at 01:18 AM

@XenosTiger, have you tried to slow down the force?

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 XenosTiger · Nov 05, 2018 at 03:13 AM 0
Share

Yes, all that slowing the force does is reduce the height the character "teleports" upwards.

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

99 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

Related Questions

2d Circle won't change size 0 Answers

triplex 2d unity game 0 Answers

I have a problem with coins 1 Answer

Multiple Cars not working 1 Answer

Why does my swiping code not work? 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