• 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 NoWayMan112 · May 22, 2015 at 09:15 AM · vector3movetowards

Object not reaching target with MoveTowards

I know this question has been asked but the answers to this question still yields the same result for me.

 void toGround()
     {
         transform.position = Vector3.MoveTowards (transform.position, pos, Time.deltaTime * speed);**strong text**
      
         if(Vector3.Distance(transform.position, pos) < 0.1f)
             DropDown = false;
     }

Basically I have a spaceship abducting an object. This method is called in the Update when the ship has left the abduction bounds and now the object is falling back to it's original position which is pos. When DropDown is set to false, it stops moving to pos, and continues on wandering in another script.

The object does return to it's position, however it just stays there and does not move. I've looked at the objects transforms and it looks like it is infinitiley going towards the new position but never reaching it.

I've also tried

 if(transform.position.x == pos.x && transform.position.x == pos.y && transform.position.z == pos.z)

However, this yeilds the same result.

What am I doing wrong?

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 duck ♦♦ · May 22, 2015 at 09:44 AM 0
Share

Looks to me like the problem lies elsewhere in your code. (eg, the logic of when toGround is called, or how DropDown is handled). With only the toGround function in your post, it's probably impossible to answer your question.

avatar image Pharaoh_ · May 22, 2015 at 09:49 AM 0
Share

Restructure your if statement as follows, just in case:

 void toGround()
 {
     if (DropDown) {
         transform.position = Vector3.$$anonymous$$oveTowards (transform.position, pos, Time.deltaTime * speed);
         if(Vector3.Distance(transform.position, pos) < 0.1f) {
             DropDown = false;
         }
             
     }
 }
avatar image duck ♦♦ · May 22, 2015 at 11:14 AM 0
Share

Just want to point out, you're correct to use a threshold, and not use the == method to check whether it's reached the target. Because you're using float values, there can be tiny inaccuracies (although these are far far smaller than 0.1).

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by abhi_360 · May 22, 2015 at 09:46 AM

 //your using
 if(Vector3.Distance(transform.position, pos) < 0.1f)
              DropDown = false;
 
 //try
 
 public float minValue;
 
 if(Vector3.Distance(transform.position, pos) < minValue)
              DropDown = false;
 
 //Tweak the minValue in the inspector

The answer to your question of why it isn't reaching the desired point is mathematical Unity uses interpolation process in MoveTowards method http://en.wikipedia.org/wiki/Interpolation Comment if want me to go into mathematical details

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 duck ♦♦ · May 22, 2015 at 11:15 AM 0
Share

This shouldn't make a difference. 0.1 is far larger than any floating point inaccuracy that you should see, unless your gameobjects are tens of thousands of units away from the origin.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Can Vector3.MoveTowards take a RaycastHit.point as a param? 1 Answer

Translate issue with Vector3.MoveTowards 1 Answer

Move Transform to Target in X seconds 3 Answers

How to set a target position in Vector3.MoveTowards using a variable 2 Answers

Shotting Or thrwoing Object wtih Vector3.MoveTowards 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