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 hoogemast · Jul 29, 2016 at 09:39 AM · transform.positionspeedtime.deltatimemovetowards

Using time.deltatime to move from coordinate to coordinate is too fast?

Hi Unity community,

at the moment i'm building something that moves from one coordinate to another. My problem is only that i want to give it a specific speed given by the user. For instance this code:

  void Start() 
  {
          private float timer = 0
   }
  void Update()
  {
          if(!positionreached)
          {
                  timer += Time.DeltaTime;
                  float step = (userdefinedspeed/100) * 4 * Time.DeltaTime;
                  transform.position = Vector3.MoveTowards(transform.position, newPosition, step);
          }
  }

In the code the userdefinedspeed is a percentage of the maximum speed (4 m/s). From here it takes a strange turn. When using this code one would expect that the speed wil be 2m/s when having a percentage of 50% as the userdefined speed. When checking the timer values though a distance of 0.5m will not be covered in 0.25 seconds but most of the time it looks like it is a factor 10 off and it takes around 0.025 seconds. This corresponds with the visual effect of the gameobject as well. Does anyone know what i'm doing wrong?

greetings and thanks for the help

Hoogemast

Comment
Add comment · Show 7
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 mrpmorris · Jul 29, 2016 at 10:25 AM 1
Share

if userDefinedSpeed is 100 then (100 / 100) * Time.deltaTime should give 1 m/s, and * 4 would give you 4 m/s - So the only thing I can guess is that your newPosition has an incorrect value.

avatar image mrpmorris · Jul 29, 2016 at 10:37 AM 1
Share

Ins$$anonymous$$d of (userDefinedSpeed / 100) try (userDefinedSpeed / 100f) - Does that make any difference?

avatar image hoogemast mrpmorris · Jul 29, 2016 at 10:43 AM 0
Share

Hi thanks for looking it up, the problem was from the user input indeed. There was an extra factor in the userdefinedspeed. Still having some little problems though since my calculated time (for intstance 100meters in 10m/s = 10seconds) is always a little higher than the actual time (9.91 or somthing). Could this be some kind of rounding error??

avatar image mrpmorris hoogemast · Jul 29, 2016 at 10:58 AM 1
Share

Did you try changing 100 to 100f as I suggested?

Show more comments
Show more comments
avatar image hoogemast mrpmorris · Jul 29, 2016 at 11:20 AM 1
Share

Thanks for all your help it is now only the standard floating point error that is the difference between my calculated time and the time in unity so It works perfecly now. I think all the little fixes fixed the code completely ;).

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bleackk · Jul 29, 2016 at 06:00 PM

You should add /4 to slow your Time.DeltaTime count slower by 4 times the normal speed. Or whatever, You can use float too so just do this.

 public float slowdown;//This way you can change this thing in your editor as i'm sure you know. Just fix it there If it's off by a factor of ten just add 10. 
 //If this doesn't work your things positioning is probably wrong. Also. Time.deltaTime is 1unit per second in floats.
 
 //Some stuff you have here
 
   void Start() 
   {
           private float timer = 0
    }
   void Update()
   {
           if(!positionreached)
           {
                   timer += Time.DeltaTime;
                   float step = (userdefinedspeed/100) * 4 * Time.DeltaTime/slowdown;
                   transform.position = Vector3.MoveTowards(transform.position, newPosition, step);
           }
   }
 

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

53 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

Related Questions

How to fix box drop via code animation? 2 Answers

Transform player to RaycastHit.point depending on speed float 1 Answer

Help using Vector2.MoveTowards 2 Answers

gradually raise moving speed but got different results on different devices 1 Answer

moving objects with transform position 2 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