Search Unity

[SOLVED] Making an object move towards and past a point.

Discussion in 'Scripting' started by Deleted User, Oct 25, 2014.

  1. Deleted User

    Deleted User

    Guest

    I am trying to make an object move towards and past a point.
    This is my script as of now, but the object stops upon reaching the point.

    Code (CSharp):
    1.         void Update ()
    2.         {
    3.                 transform.position += ((Vector3)targetPos - transform.position).normalized * speed * Time.deltaTime;
    4.         }
    5.        
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    It stops because as soon as you pass it, the calculated direction is the opposite. So you'd move back into the direction where you came from until you pass it again, then move forward again, back again etc.

    You shouldn't recalculate the direction this way. If your target doesn't move, calculate it once and save it somewhere and keep your object move into that direction.
     
    e111s1, Deleted User and Magiichan like this.
  3. Deleted User

    Deleted User

    Guest

    Thanks. That solved it.
     
    Suddoha likes this.