How to stop moveTowards?

I noticed Vector3.moveTowards causes the gameobject to constantly move to the same position even if i control it to go another place with key controls and I want it to stop when it reaches the gameobject or position.

can you post your code?

normaly you can make if statements which is set to true or false.
or you can set targetpos to transform.pos when you don’t push any button.
atm i don’t know what your button inputs do to the targetposition or how you handle it that you have this problems :slight_smile:

Hi, I’m currently trying to create path nodes whereby the user’s character will move to these predetermined points and the only thing that the user can control is the speed but I have not yet started on doing the speed control yet as I am still trying to fiddle with the movement first.

using UnityEngine;
using System.Collections;

public class MoveTowardsDirection : MonoBehaviour
{
   
    public Transform target1;
    public Transform target2;
    public float speed;
    public Transform target;
   
   
    // Use this for initialization
    void Start ()
    {
       
    }
   
    // Update is called once per frame
    void Update ()
    {
        MoveToTarget ();
       
    }
    void MoveToTarget ()
    {
        //float step = speed * 10;

        for (int i = 0; i < 3; i++) {
           
            if (i == 1) {
               
                target = target1;
                transform.position = Vector3.MoveTowards (transform.position, target.position, speed);
                if (GameObject.FindGameObjectWithTag ("Bike").transform.position == GameObject.FindGameObjectWithTag ("NANANA Batman").transform.position) {
                    //GameObject.FindGameObjectWithTag("Bike").transform.Rotate(0,-10,0);
                   
                    transform.LookAt (target2);
                }
            } else if (i == 2) {
               
                target = target2;
                transform.position = Vector3.MoveTowards (transform.position, target.position, speed);
                if (GameObject.FindGameObjectWithTag ("Bike").transform.position == GameObject.FindGameObjectWithTag ("Stop sign").transform.position) {

                }
               
            }
           
        }

       
    }   
   
}

As far as your code is working you will never reach your target. because every frame your gameobject is moving to target1 and than to target2.

Hi, I’m relatively new to programming so I do not know of a way to solve this, would you be able to provide a better way to go around doing this?

so when i think correct, you want to first reach target1 and than target2?

Yes, the gameobject would move progressively from the start point to towards target1 and then to target2.

Something like path nodes in unreal engine

is the Bike tagged gameobject this movement script is attached to?
and batman is your target1 , and stop sign the target2 gameobject ?

and if you want waypoints try out
https://unity3d.com/learn/tutorials/modules/beginner/navigation/navmesh-agent

doube post?

Hi currently this is my scene view, the bike is the main character, and the batman and stop signs are going to be the waypoints.
2346513--158820--1321.PNG

your problem is solved in your other post :wink:

1 Like

Hi, the nav mesh works perfectly, thanks alot!