Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 dkfister · Mar 05, 2014 at 03:33 PM · c#charactercontrollertimetime.deltatime

Time.DeltaTime doesn't seem to be correctly

I'm currently developing a game, where I have my player controller that can move.

In the Editor, the speed is normal as expected. When I'm building the game to a Standalone build, and runs it in Windowed Mode, it also runs fine, same speed and framerate independent.

However, if I suddenly go to fullscreen with the exported standalone build, all of a sudden everything seems alot faster, and not framerate indepedent at all.

For better understanding of the problem, download the build here: http://kevinjp.dk/games/lightsource/windows/LightSource_Alpha10.zip

It's worth noting that i'm using the Character Controller Component for my movement.

My movement source code:

 private void Movement() {
     //Z-Axis movement, forward and backwards. Adds current acceleration to the currentSpeedVertical.
     currentSpeedVertical += acceleration * Input.GetAxis("Vertical");
     currentSpeedVertical = Mathf.Clamp(currentSpeedVertical, -maxSpeed, maxSpeed);
 
     //X-Axis movement, left and right. Adds current acceleration to the currentSpeedHorizontal.
     currentSpeedHorizontal += acceleration * Input.GetAxis("Horizontal");
     currentSpeedHorizontal = Mathf.Clamp(currentSpeedHorizontal, -maxSpeed, maxSpeed);
 
     //Y-Axis movement, up and down. Adds current acceleration to the currentSpeedUpDown.
     currentSpeedUpDown += Input.GetKey(KeyCode.Space) ? acceleration : 0;
     currentSpeedUpDown -= Input.GetKey(KeyCode.LeftShift) ? acceleration : 0;
     currentSpeedUpDown = Mathf.Clamp(currentSpeedUpDown, -maxSpeed, maxSpeed);
 
     //Checks if player is not moving vertically, if that's the case, deaccelerate our values.
     if(Input.GetAxis("Vertical") == 0 && currentSpeedVertical != 0) {
         if(currentSpeedVertical > 0) currentSpeedVertical -= acceleration;
         if(currentSpeedVertical < 0) currentSpeedVertical += acceleration;
         if(Mathf.Abs(currentSpeedVertical) < acceleration) currentSpeedVertical = 0;
     }
 
     //Checks if player is not moving horizontally, if that's the case, deaccelerate our values.
     if(Input.GetAxis("Horizontal") == 0 && currentSpeedHorizontal != 0) {
         if(currentSpeedHorizontal > 0) currentSpeedHorizontal -= acceleration;
         if(currentSpeedHorizontal < 0) currentSpeedHorizontal += acceleration;
         if(Mathf.Abs(currentSpeedHorizontal) < acceleration) currentSpeedHorizontal = 0;
     }
 
     //Checks if player is not holding space or leftshift, if that's the case, deaccelerate our values.
     if(Input.GetKey(KeyCode.Space) == false && Input.GetKey(KeyCode.LeftShift) == false && currentSpeedUpDown != 0) {
         if(currentSpeedUpDown > 0) currentSpeedUpDown -= acceleration;
         if(currentSpeedUpDown < 0) currentSpeedUpDown += acceleration;
         if(Mathf.Abs(currentSpeedUpDown) < acceleration) currentSpeedUpDown = 0;
     }
 
     //Put it all together in a movement variable.
     Vector3 movement = new Vector3(currentSpeedHorizontal, currentSpeedUpDown, currentSpeedVertical);
 
     //Move the actual character controller with the correct values converted
     //in a TransformDirection, and multiple that with deltaTime to make it framerate independent.
     cc.Move(transform.TransformDirection(movement) * Time.deltaTime);
 
 }
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
2
Best Answer

Answer by Hoeloe · Mar 05, 2014 at 03:57 PM

This is because, while you are applying the movement using Time.deltaTime, you are still accelerating it dependent on the framerate. Think about it like this: If your acceleration is 1, then since you are adding to currentSpeedUpDown without multiplying by Time.deltaTime, then in 60 frames, your speed will be 60. At 60 frames per second, this will take 1 second. At 30 frames per second, this will take 2 seconds - the acceleration is not framerate independent! Now you multiply that by Time.deltaTime to apply it, and that's all well and good, but your speed is still increasing dependent on the framerate, so naturally your acceleration will be faster, if not your overall speed.

What you really need to do is multiply the acceleration components by Time.deltaTime when you add them to the speed - this ensures that acceleration is also framerate independent, and it should fix your issue.

Comment
Add comment · Show 5 · 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 dkfister · Mar 05, 2014 at 04:01 PM 0
Share

So what your saying is, that I should actually change the $$anonymous$$ovement Variable line to this:

Vector3 movement = new Vector3(currentSpeedHorizontal Time.deltaTime, currentSpeedUpDown Time.deltaTime, currentSpeedVertical * Time.deltaTime);

avatar image Hoeloe · Mar 05, 2014 at 04:06 PM 0
Share

No, absolutely not. I'm saying you need to change lines like this:

 currentSpeedUpDown += acceleration;

To this:

 currentSpeedUpDown += acceleration * Time.deltaTime;

Time.deltaTime is the change in the computer's internal clock since the last update. You need to apply it to changes you've made to your values, not to the values themselves.

avatar image dkfister · Mar 05, 2014 at 04:20 PM 0
Share

Think I defintely got it now. Ofcourse, after I added the Time.deltaTime where I am making changes to my currentSpeed variables, I'd have to change the acceleration values a little bit, but I guess that's to be expected?

It works now! I really appreciate your help man!

avatar image Hoeloe · Mar 05, 2014 at 04:28 PM 0
Share

Yup, that's to be expected, but it should mean it's framerate independent.

avatar image dkfister · Mar 05, 2014 at 04:35 PM 0
Share

It definitely is. Runs perfectly everywhere :)

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

21 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

Related Questions

How to create an Idle counter ? 2 Answers

C# FP_Camera not following Character Controller 0 Answers

Having trouble increasing the spawning speed of explosions 0 Answers

How would I use this to check if the player has a key and is touching a door? 1 Answer

A node in a childnode? 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