Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Rukey4 · Jan 08, 2015 at 11:16 PM · javascriptarraymovetowards

how to check when MoveTowards has finished

Hello,

I've got a small portion of code which does move a GameObject to a random destination (Specified by the target in the array). It all works fine, I just want to know how to check when the "MoveTowards" has finished, so I can find a new destination or target. The last pseudo 'if' statement is what I'm trying to achieve. Thanks a lot!

 #pragma strict
     
     var target : Transform[];
     var isMoving : boolean = false;
     
     var speed : float = 5.0f;
     
     var newTarget : Transform;
     
     function Update()
     { 
         if(isMoving == false)
         {
             newTarget = target[Random.Range(0,target.length)];
             isMoving = true;
         }
         
         transform.position = Vector3.MoveTowards(transform.position, newTarget.position, speed * Time.deltaTime);
 
         if(MoveTowards has finished)
         {
            isMoving = false;
         }
     }



Comment
Add comment · Show 2
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 Scribe · Jan 08, 2015 at 11:21 PM 0
Share

you could create your own Vector3.Approximately method, by just checking the x, y and z floats all return true from $$anonymous$$athf.Approximately, or you could save the start position and work out how long it will take to travel the distance given your speed (doesn't work if you have variable speed), or you could use lerp ins$$anonymous$$d and check when your t value given is more than or equal to one?

avatar image Rukey4 · Jan 08, 2015 at 11:25 PM 0
Share

HYPERSAVV, superb answer. The answer was staring me right in the face... Thanks for your time! Works perfectly. I'll mark your comment as a chosen answer if you convert it. Thanks.

2 Replies

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

Answer by HYPERSAVV · Jan 08, 2015 at 11:22 PM

Just check to see if transform.position equals newTarget.position

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
17

Answer by Juice-Tin · Jan 08, 2015 at 11:36 PM

 if(Vector3.Distance(transform.position, newTarget.position) < 0.1f){
 //It is within ~0.1f range, do stuff
 }
 
Comment
Add comment · Show 2 · 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 2dgamemania · Feb 17, 2017 at 06:08 PM 0
Share

thanks, i've been hours looking for this solution

avatar image Chambers88 · Dec 18, 2020 at 02:15 AM 1
Share

Since it moves speed * Time.deltaTime units per frame, you could check if the distance is smaller than that to have a more precise result.

 if(Vector3.Distance(transform.position, newTarget.position) < speed * Time.deltaTime){
   //do stuff
 }

Another cool thing you can do is make your own $$anonymous$$oveTowards function that returns whether it reached that position or not. This is good because you will get the result in the same frame as it overshoots the destination, and not one frame after:

 private bool $$anonymous$$oveTowards(Transform t, Vector3 target, float speed) {
     if (Vector3.Distance(transform.position, target) < speed) {
         t.position = target;
         return true;
     } else {
         t.position = Vector3.$$anonymous$$oveTowards(t.position, target, speed);
         return false;
     }
 }

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

29 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

Related Questions

Javascript Script for Object Follow? 0 Answers

Static Array JS 0 Answers

Set variables in an array dependent on which scene the player is in. 1 Answer

Disabling random GUI Button from array 0 Answers

Need a function to loop through an array to check tags 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