• 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 Attapi0 · Feb 15, 2014 at 04:01 PM · timemovement scriptframeratedeltatime

I can't get framerate independence to work...

So I have a movement script for a rigidbody. I use Time.deltaTime for everything but it definitely isn't framerate independent. Lower framerates make you move faster, and higher framerates make you move slower. Check out my code.

 #pragma strict
 
 var MovementSpeed = 7000;
 private var MovementVer : int;
 private var MovementHor : int;
 private var distGround : float;
 var MaxSpeed : float;
 var jumpSpeed : float;
 
 function Start(){
     distGround = collider.bounds.extents.y;
 }
 
 function IsGrounded() : boolean {
     return Physics.Raycast(transform.position, -Vector3.up, distGround + 0.1);
 }
 
 function Update() {
     //setting up the movement variables
     MovementVer = Input.GetAxis("Vertical") * MovementSpeed * Time.deltaTime;
     MovementHor = Input.GetAxis("Horizontal") * MovementSpeed * Time.deltaTime;
 
 
     //Stops the character from sliding around when you aren't moving
     if(Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0) {
     
         rigidbody.drag = 1;
         }
     else{
         rigidbody.drag = 10;
         }
     if(Input.GetAxis("Jump") && IsGrounded()){
         rigidbody.drag = 0;
         rigidbody.velocity.y = jumpSpeed;
         }
     if(IsGrounded() == false){
         rigidbody.drag = 0;
         }
     
     if (IsGrounded() == false){
         MovementSpeed = 2;
         }
     if (IsGrounded() == true){
         MovementSpeed = 7000;
         }
 }
 function FixedUpdate () {
 
         rigidbody.AddRelativeForce(Vector3(MovementHor, 0, MovementVer));
         
 }
 
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

1 Reply

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

Answer by whydoidoit · Feb 15, 2014 at 04:04 PM

You are moving the item in physics FixedUpdate but you are using Time.deltaTime to work out the horizontal speed when it needs to be Time.fixedDeltaTime because of where you actually apply the movement.

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 Attapi0 · Feb 15, 2014 at 07:07 PM 0
Share

Thank you so much!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

19 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

Related Questions

Using Time.DeltaTime doesn't result in framerate independence 1 Answer

Framerate Independent Firing Rate (delta time?) 2 Answers

Jump with mathf.lerp problem 2 Answers

Game over scene when the time is up ? 2 Answers

How to synchronize frame updates across a network of multiple (UDP) clients? 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