# Moving an object to random points Unity3d c#

So here is the problem I want to change the position of gameObject to specific points that I declared with empty objects randomly but the problem is the moment the object reaches one of the points it starts shaking around and not doing the same thing again here is my code

public class EnemyController : MonoBehaviour {

public Transform[] movePoints;
public float speed;
private int amount;
private Transform currentTarget;

void Start () {
randoming ();

}

void Update () {
Movement ();
}

void randoming(){
amount = Random.Range (0, movePoints.Length);
currentTarget = movePoints[amount];
}

void Movement(){
transform.position = Vector3.MoveTowards (transform.position, currentTarget.position, speed * Time.deltaTime);
StartCoroutine (startMoving());
}

IEnumerator startMoving(){
yield return new WaitForSeconds (5.05f);
randoming ();
}


}

• Are you starting a coroutine each frame? Is that the intention? – Vaillancourt Aug 19 '19 at 0:33

You are calling Movement every Update frame, so you are starting a new coroutine for startMoving several times a second. You won't see the effects of this at first because each one is waiting 5.05 seconds, but after that I expect the object starts shaking because randoming will be called many times per second as all those coroutines finish. In other words, your object appears to be shaking because the currentTarget, towards which it is supposed to move, is being changed too rapidly for it to move significantly in any one direction.

It would be easier for you to just use a float as a timer to count down when you want to change targets instead of using the coroutine.

public class EnemyController : MonoBehaviour {

public Transform[] movePoints;
public float speed;
private int amount;
private Transform currentTarget;
private float timer = 0f;

void Start () {
randoming ();
}

void Update () {
Movement ();
timer += Time.deltaTime;
if (timer > 5.05f)
{
randoming();
timer = 0f;
}
}

void randoming() {
amount = Random.Range (0, movePoints.Length);
currentTarget = movePoints[amount];
}

void Movement() {
transform.position = Vector3.MoveTowards (transform.position, currentTarget.position, speed * Time.deltaTime);
}

}


if you only want your objects to move to a random point i believe its a wrong approach. just define a general destination Like Below:

public Transform destination;


first set the Transform in inspector on any other way you know. then just in the start section make you object to move to that destination. for this you have to define another temp Transform Variable. its the whole code:

[SerializeField] private   Transform Destination;
[SerializeField] private float speed;
private Vector3 tempDestination;

Start()
{
tempDestination = new Vector3(Destination.position.x+Random.Range(0,5),
Destination.position.y + Random.Range(0, 5),
Destination.position.z + Random.Range(0, 5) );
}

Update()
{
transform.position = Vector3.MoveTowards(transform.position, tempDestination, speed);

}