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 Novahkiin · Jan 24, 2018 at 10:54 PM · movementmovement scriptframerateframes per second

Making my Player's movement frame rate independent

I'm still pretty new to Unity and just learned how to get diagonal movement working in a 2D space using the horizontal and vertical axes but I'm having trouble making the movement frame rate independent.

 public class PlayerController : MonoBehaviour {
 
     // Player's movement speed
     public float moveSpeed = 2.0f * Time.deltaTime;
 
     void Update() {
         // Float value obtained from the horizontal and vertical axes
         float movementInputX = Input.GetAxisRaw("Horizontal");
         float movementInputY = Input.GetAxisRaw("Vertical");
 
         Vector3 direction = new Vector3(movementInputX, movementInputY);
 
         direction.Normalize();
         direction *= moveSpeed;
         transform.position += direction;
     }
 }

I've tried using Time.deltaTime where I initialized moveSpeed but got an error that read: "get_deltaTime is not allowed to be called from a MonoBehavior constructor (or instance field initializer), call it in Awake or Start instead."

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
2
Best Answer

Answer by Kishotta · Jan 24, 2018 at 11:09 PM

The issue is that Time.deltaTime is not a constant value. It represents the amount of time taken to render the previous frame. It will likely be slightly different every Update loop.

Say, for the sake of simple math, that your game runs at 60fps and you want to move your player 6 units per second. That means you need to move your player 6 units every 60 frames. So in 1 frame, that 0.1 units. That's all well and good, but say something happens in your game that dips the frame rate for a quarter of a second. Now, for a short amount of time, the game is only running at 45fps. During those frames, the player should move 0.1333 units per frame.

To do this, when you multiply your direction by moveSpeed you should also multiply it by Time.deltaTime.

 direction *= (moveSpeed * Time.deltaTime);
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 Novahkiin · Jan 25, 2018 at 12:01 AM 0
Share

Thanks, this worked!

avatar image
0

Answer by actionjdjackson · Apr 11, 2020 at 06:06 PM

I've tried this but can't seem to get more than a dead crawl out of my code:

 public void Walk()
     {
         float controlFlowHorizontal = CrossPlatformInputManager.GetAxis("Horizontal");    //between -1 and +1
         float controlFlowVertical = CrossPlatformInputManager.GetAxis("Vertical");
         Vector2 PlayerVelocity = new Vector2(controlFlowHorizontal * walkSpeed * Time.deltaTime, controlFlowVertical * walkSpeed * Time.deltaTime);
         myRigidBody.velocity = PlayerVelocity;
     }

I've tried walkSpeeds from 75 - 3000 and no dice, it still just barely moves along

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 actionjdjackson · Apr 11, 2020 at 06:25 PM 0
Share

$$anonymous$$y mistake! I had another piece of code feeding into walkSpeed a different value than in the inspector.

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

116 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 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 Vector Movement,Vectorel Movement with Buttons 2 Answers

HOW TO MAKE A NPC GO FORWAR 0 Answers

How can i smooth out the rotation? 1 Answer

Implementing Counter-Movement 0 Answers

How to calculate an accurate frame duration? 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