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 hh54188 · Mar 12, 2017 at 11:39 AM · 2d-physics

Unity 2D physics: problem about gravity simulation

What I am trying to do is: when user press space button, I will give the game object an vertical velocity for jump, and minus an gravity number in each frame (in update function) to simulate gravity effect. Here is my code:

 float gravity = -10;
 float jumpVelocity = 200;
 
 float logicSpeedY = 0;
 float actualSpeedY = 0;
 
 public int targetFPS = 30;
 bool jumped = false;
 
 void Awake()
 {
     QualitySettings.vSyncCount = 0;
     Application.targetFrameRate = targetFPS;
 }
 
 void Update()
 {
     if (Input.GetKey(KeyCode.Space))
     {
         logicSpeedY = jumpVelocity;
         jumped = true;
     }
 
 
     if (jumped)
     {
         logicSpeedY = logicSpeedY + gravity;
         actualSpeedY = logicSpeedY * Time.deltaTime;
 
         transform.Translate(new Vector3(0, actualSpeedY, 0));
     }
 
 }

These code worked in a way. But the problem is, the game object jump distance is different in different FPS.

I think I have avoid this because the speed multiplied by Time.deltaTime. So why this still happened ? How can I let the jump distance is equal in different FPS ?

Comment
Add comment · Show 4
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 snailguides · Mar 12, 2017 at 01:27 PM 0
Share

Because when the FPS is low, Time.deltaTime become large, then you hit jump at that frame, the player should jump higher. Jumping with Time.deltaTime should be avoid. I guess you are new to Unity, I recommend to use the built-in physics (rigidbody/charactercontroller) ins$$anonymous$$d of implementing your own.

avatar image hh54188 snailguides · Mar 12, 2017 at 02:12 PM 0
Share

But if I handle horizontal speed in this way: input.x * speed * Time.deltaTime, it could move in constant speed in different FPS, why vertical can not ?

avatar image hexagonius hh54188 · Mar 12, 2017 at 02:51 PM 0
Share

because the horizontal movement is a straight line while a jump is an arc. think of the way you calculate movement as if you were adding distances of different length on top of each other. while in a straight line, there's no offset in any other axis, but with a jump arc every slight difference leads to a different position from which any future change differs.
if you want to implement the jump yourself use a formula describing the jump arc where you can pass in the current time and it gives you the position of the arc.

Show more comments

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

98 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

Related Questions

Rigidbody2D.Moveposition is acting more like add force after 5.2 0 Answers

hinge joint in character 0 Answers

A 2D Character Controller with Rigidbody2D.AddForce 2 Answers

2d Grid movement collision errors,raycast detecting walls grid movement 1 Answer

How to shoot object from rotator ? 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