Search Unity

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

Discussion in 'Scripting' started by Trild123787898, Mar 20, 2019.

  1. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    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?
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    You may start a corooutine and regulary check if the moving object position is close engough to target. Or, you may create a event on that object and subscribe to it. Fire the event from the moving function when it ha approached the target.
     
  3. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    have documentation? or what to look for?
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Check the distance from the object to its goal against some thereshehold you define, say .1m, if its smaller, you're there.
     
  5. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
  6. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Don't ever use Vector3.Distance, its excatly the same as saying (firstVector - secondVector).magnitude with an extra func call, furthermore, use the sqaure magnitude and compare against a squared version of your distance, it costs you 1 multiplication(squaring the distance) but saves you a root function, a net gain.