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 DREZZ3R · Aug 18, 2016 at 04:00 PM · movementphysicsmovement scripttime.deltatime

How Time.deltaTime affects movement ?

 float speed = 20;
 Rigidbody2D rb2D;
 rb2D = GetComponent<Rigidbody2D>();
 Vector2 moveDir = new Vector2(Input.GetAxisRaw("Horizontal") * speed, rb2D.velocity.y); 
 rb2D.velocity = moveDir;

This a simple code to move horizontally(it works) but I heard that it is very important to use Time.deltaTime in order to move by time and not by the speed of your CPU however when I put like this:
Vector2 moveDir = new Vector2(Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime, rb2D.velocity.y);

Since Time.delta Time gives me a number less than 0 and logically he moves so much slower almost nothing. SO as I said how do I implent the Time.deltaTime if it's necessary of course.

Thanks for reading.

Comment
Add comment · Show 3
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 meat5000 ♦ · Aug 18, 2016 at 04:22 PM 1
Share

https://unity3d.com/learn/tutorials/topics/scripting/delta-time

avatar image DREZZ3R · Aug 18, 2016 at 05:03 PM 0
Share

@meat5000

Thanks for your reply. To resolve the problem I just increased speed (from 20 to 1500) and now runs fine I guess put:

 speed = 1500//or any increased speed;
 Vector2 moveDir = new Vector2(Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime, rb2D.velocity.y);

Yet, I think it's a bit strange. Never saw a person putting such high value just to move but oh well...

avatar image DREZZ3R DREZZ3R · Aug 18, 2016 at 05:01 PM 0
Share

I noticed another problem, even with increased speed, when I turn off vsync, the game has more FPS but the character runs so slow. It shouldn't happening since this method should be working by time and not FPS.

3 Replies

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

Answer by tanoshimi · Aug 18, 2016 at 07:31 PM

If you want framerate-independent kinematic movement (i.e. you're moving the object, via Translate() or explicitly setting the value of transform.position) then multiplying by Time.deltaTime is essential.

But in this example, you're not moving the object; you're setting the rigidbody's velocity and letting the Physx engine determine how far the object should move in each frame accordingly. It doesn't make any difference if you set a velocity of 20 once a second or 50 times a second - it's still going to be 20 in every single frame when the physics calculation is performed, and that's why you've not noticed a difference in this case.

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 DREZZ3R · Aug 19, 2016 at 01:08 PM 0
Share

Very good explained. Thanks.

avatar image
1

Answer by ZammyIsOnFire1 · Aug 18, 2016 at 05:04 PM

Your speed modifier should be used to modify your speed. Use it as a per second movement speed modifier.

So if you want to move 1 unit per second you can leave it at 1. If you want to move 20 units per second leave at 20.

Fast and slow depends on your camera settings and how much is 1 unit in the game.

Comment
Add comment · 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
-1

Answer by DREZZ3R · Aug 18, 2016 at 07:19 PM

Well guys, I discovered that for this movement method you don't need Time.deltaTime. I did a test with a timer. The test involved the player to reach a certain location in a straight path and when he reaches the goal, it shows in the console how much time I spent reaching the goal and this was the results;

  • 3.403seg - locked at 60fps

  • 3.400seg - unlocked(2800fps+)

  • 3.424 - vsync

In conclusion, this script for moving(the very first one I posted)doen't need Time.delaTime. It's just a dfference of MilliMilliSeconds.

@meat5000 commented the answer with a video.

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Quake Movement in Unity 1 Answer

isKinematic does not always work 0 Answers

My player's movement is slippery 1 Answer

Jump only sometimes works and player doesn't always jump high? 1 Answer

Non slippery movement 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