• 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
-2
Question by Trild123787898 · Mar 20, 2019 at 02:56 PM · transformscript.movetarget

How do I know that the object has reached its goal?

I have an object which, when I press a key, should move to the target, how can I find out in the script that the object has reached its goal?

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

3 Replies

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

Answer by highpockets · Mar 20, 2019 at 05:14 PM

Use lerp and when the time value reaches 1, you have reached your target:

 float timeCount = 0.0f;
 
 void Update(){
 
 object.transform.position = Vector3.Lerp(object.transform.position, targetPos, timeCount);
 
 if(timeCount >= 1.0f){
 //target reached
 }
 timeCount = timeCount + Time.deltaTime;
 
 }
Comment
Add comment · Show 16 · 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 highpockets · Mar 20, 2019 at 05:20 PM 0
Share

The way I wrote the code is so the timeCount will reach 1 in 1 second, but you can of course change that by changing the way the timeCount updates. For example:

 timeCount = timeCount + (Time.deltaTime * 0.5f);

The above will ins$$anonymous$$d reach 1 in 2 seconds

avatar image Trild123787898 · Mar 20, 2019 at 05:35 PM -1
Share

Thanks for the answer! Only vibrates here error error CS0117: 'object' does not contain a definition for 'transform'

avatar image highpockets Trild123787898 · Mar 20, 2019 at 05:40 PM 0
Share

it was just an example, but if this script is on the object you want to move, you can just put:

transform.position = Vector3.Lerp(transform.position, targetPos, timeCount);

avatar image Trild123787898 highpockets · Mar 20, 2019 at 06:29 PM -1
Share

another error CS1503 error: Argument 2: cannot convert from 'UnityEngine.Transform' to 'UnityEngine.Vector3'

Show more comments
avatar image highpockets · Mar 20, 2019 at 07:44 PM 0
Share

Why did somebody give a negative vote to my answer? I imagine somebody else who gave an answer is displeased, I was simply providing an additional solution here since there are many ways to skin a cat and some cats are easier to skin certain ways than others. People in the future will come to this post and see multiple solutions to accomplish something and everybody will want to approach the situation differently. The lerp function is a good answer to this question just as the other answers have their value. If somebody has a logical explanation why lerp is not a good approach, please explain? And if your explanation makes any sense at all, I will remove the answer..

avatar image xxmariofer highpockets · Mar 20, 2019 at 08:02 PM 0
Share

not sure why, i upvoted because this is a correct way of doing this, just one thing, the code you posted is for non linear speed, i would suggest not doing it that way since managing the speed curve becomes really hard, if your idea was to make a linear movement you are missing a startpoint, that must be constant for all the movement duration.

avatar image highpockets xxmariofer · Mar 20, 2019 at 08:21 PM 0
Share

@xxmariofer is right. $$anonymous$$y code will have the object slow down as it gets closer to the target. For linear, you would need to put a start position that stays constant for the duration of the move.

Show more comments
avatar image
0

Answer by dan_wipf · Mar 20, 2019 at 03:23 PM

hm i’d use a Vector3.Distance for +- accurate equation of the target => if(Vector3.Distance(objectPosition,targetPosition) <0.01f)


or


if(objectPos == TargetPos) but sometimes the target position is not the same as object position, so you use the first code

Comment
Add comment · Show 4 · 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 Trild123787898 · Mar 20, 2019 at 04:11 PM 0
Share

thanks for the answer ! I will try

avatar image TreyH · Mar 20, 2019 at 05:38 PM 1
Share

Careful, as equality of structs is equality of values for value types and references for reference types.

edit: see hellium's reply

avatar image Hellium TreyH · Mar 20, 2019 at 06:50 PM 1
Share

Comparing two vectors is fine. Unity has overloaded the == operator:

     public const float kEpsilon = 0.00001F;
     
 public static bool operator==(Vector3 lhs, Vector3 rhs)
     {
         // Returns false in the presence of NaN values.
         return Sqr$$anonymous$$agnitude(lhs - rhs) < kEpsilon * kEpsilon;
     }
avatar image TreyH Hellium · Mar 20, 2019 at 07:07 PM 0
Share

Huh, had no idea. +1

avatar image
0

Answer by dargonknight · Mar 20, 2019 at 03:29 PM

@Trild123787898 i agree with @dan_wipf 's answer but an other option would be to simply add a trigger and check the on trigger function

     private void OnTriggerEnter(Collider other)
     {
         if (other.tag == "Target")
         {
 //ur action goes here
         }
     
     }
Comment
Add comment · Show 4 · 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 Trild123787898 · Mar 20, 2019 at 04:11 PM 0
Share

thanks for the answer ! I will try

avatar image dan_wipf · Mar 20, 2019 at 04:24 PM 0
Share

well yes, but this only works when the script is attached to the gameobject which is moving towards the target, or if the script is attached to the targets object

avatar image dan_wipf dan_wipf · Mar 20, 2019 at 04:31 PM 0
Share

and it will be not that accurate aswell as vector3 /distance comparison.. the objects position will then be at the BB’s of the collider somwhere: for example:


if you have a size one cube as object to move and aswell as target. and it enter’s the trigger of the targets cube, from starting pos vector3(-10,0,0) to target(vector3(0,0,0) the position of enterinf the trigger zone will then be vector3(0.5,0,0) and the actual position of the object to move will be vector3(1,0,0)

avatar image dargonknight dan_wipf · Mar 20, 2019 at 10:19 PM 0
Share

@dan_wipf using a small sphere collider can fix the problem, when giving advise to beginners i believe that it's best to let them start by easy things that can see visually, understand and learn from to do bigger things my option works, it can be seen in the editor, it introduces the trigger etc... that's why i sent it.

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

144 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 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

Accessing boolean properties from array of transforms 1 Answer

Lock rotation axis? 4 Answers

Instantiate VS Transform 1 Answer

Moving an object to points 1 Answer

Bullet Prefab spawning in multiple random positions and rarely the correct one (Unity 3D) 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