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 hillsy7 · Nov 11, 2018 at 06:05 PM · vector2movetowards

Vector2.MoveTowards not working

Hi all,

I'm sure this is a rookie mistake but I've followed another example and the solution Given seems to be exactly what I've got - I'm missing something obvious. Here is the code:

     void Start () {
             CurrPos = transform.position;
             NewPos = transform.position + (transform.up*4.0f);
         }
         
         // Update is called once per frame
         void Update () {
             Debug.Log ("CurrPos: " + CurrPos + ", NewPos: " + NewPos + ", ActualPos" + transform.position);
     
             transform.position = Vector2.MoveTowards (CurrPos, NewPos, 10.0f * Time.deltaTime);
 }

The Debug outputs all the correct values, but the transform.position never changes.

Apologies for the noob question, but any help would be appreciated.

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
0

Answer by JVene · Nov 11, 2018 at 06:19 PM

Consider the 3rd parameter you're providing to MoveTowards, 10.0f * Time.deltaTime. When the engine is limiting frame rate to the refresh rate, this usually means something like a constant 60 frames per second, such that Time.deltaTime is always 1/60th of a second (assuming the graphics isn't heavy so the rate is always high).


That would mean that 10.0f * Time.deltaTime always returns the same number. MoveTowards is a version of a lerp, an interpolation between two values ( CurrPos & NewPos ) based on a ratio in the range of 0 to 1. The closer to 0, the closer the result is CurrPos, the closer to 1.0 the closer the result is to NewPos. However, in your example, it is likely that this value, the ratio you're providing, is stick close to zero - about .0.166667.


What you need to do is increment time. When this motion begins, the value you provide as this ratio should be about 0. Create and set an "accumulatedTime" float to zero to begin this. If you want the event to take 10 seconds, then you want this ratio to reach 1.0 when 10 seconds has elapsed. So, accumulatedTime / 10.0f would do that.


What you use Time.deltaTime for is when INCREMENTING accumulatedTime. It represents how much time passed since the last update. So, accumulatedTime += Time.deltaTime should be performed after you call MoveTowards.


This also implies that you should limit the accumulated time. When the total time reaches or exceeds 10.0f (or whatever factor you choose), the ratio supplied to MoveTowards should be 1.0f, and the animation should stop - it reached the goal.

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

94 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

Related Questions

conserve momentum of vector3.moveTowards in 2D game 0 Answers

Moving an UI element from point A to point B 1 Answer

Trail not visible when using Vector2.MoveTowards() 0 Answers

Put an offset distance to gameobject that follows player 2 Answers

How to store variables in start method 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