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
1
Question by jebemti · Feb 26, 2016 at 12:37 AM · movementcharactercontrollercharacter controllerframerateindependent

Framerate independent Movement

In my project I have Unity's CharacterController. This is the code that made it move. It's working great when using fixed timestep.

 vel.x *= friction;
 vel.z *= friction;
 
 vel.x += input.x;
 vel.z += input.z;

 cc.Move (vel);

I want my game to support unlocked framerate so the character's movement should be updated on per-frame basis.

The problem is that the acceleration is really inconsistent. Eventually all framerates hit the maximum speed (they get closer to it every frame), but 1000 FPS reaches it almost instantly, while 15 FPS takes a long time to accelerate. After pondering about it, it makes sense, but I wasn't able to make the acceleration consistent...

What is the mathematical solution to this? Any tips are appreciated!

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
1

Answer by phil_me_up · Feb 26, 2016 at 01:11 AM

All you need to do is to take the time between the current and previous frame. In unity you can access that through Time.deltaTime.

Now you have that, simply multiply it by your movement and it'll be smooth(as a larger delta time will mean longer between frames, and hence the actual movement is increased).

Basic example, if you want something to move 10 units every second, you'd have something like:

pos += 10.0f * Time.deltaTime;

obviously adjust as needed and covert for vectors where necessary.

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 jebemti · Feb 26, 2016 at 01:42 PM 0
Share

This achieves consistent change in position, but what I ask is a consistent change in velocity and ultimately the position.

avatar image
0

Answer by Eno-Khaon · Feb 26, 2016 at 01:11 AM

 vel.x += input.x;
 vel.z += input.z;

These are not framerate independent lines. To make them framerate independent, you need to factor in the time between frames. What this means, though, is that you'll also want a multiplier for the actual rate of speed gain. For example:

 vel.x += input.x * speedMod * Time.deltaTime;
 vel.z += input.z * speedMod * Time.deltaTime;

When you multiply by Time.deltaTime, you're effectively changing the value of your input (i.e. inputs x and z) to a rate of X per second. As an example, your inputs x and z were updating velocity at a rate of 1 per frame, so a framerate of 60 means 60 per second.

But that's why I added "speedMod" as an example multiplier. It takes the input of (likely up to) 1 per second and gives it a multiplier to reach an intended pace.

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 jebemti · Feb 26, 2016 at 01:44 PM 0
Share

I tried this but didn't get consistent results.

Also shouldn't the amount of friction also be based on the delta time?

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

48 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

Related Questions

Strange Character Controller Behavior Caused by Simulated Gravity and Ground Check 0 Answers

How can I make my FPS player move towards the direction it is facing 1 Answer

Continuously rotate a gameobject on 1 axis perpendicularly to another gameobject's normal? 1 Answer

Framerate independent character movement 1 Answer

Player acts weirdly when I release movement input 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