# Enemy beelines before wandering in 2d topdown

I'm making a top down shooter in Unity. The enemy AI travels straight to the middle of the screen before actually starting wandering when I put in this code. What can I do to fix this? (I'm using Unity 2019.4.28f1 if that helps). I copied the code in this video.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyWander : MonoBehaviour
{
[SerializeField]
float speed;
[SerializeField]
float range;
[SerializeField]
float maxDis;
Vector2 Waypoint;
// Start is called before the first frame update
void Start()
{
setNewDes();
}

// Update is called once per frame
void Update()
{
transform.position = Vector2.MoveTowards(transform.position, Waypoint, speed * Time.deltaTime);
if(Vector2.Distance(transform.position,Waypoint) < range)
{
setNewDes();
}
}
void setNewDes()
{
Waypoint = new Vector2(Random.Range(-maxDis, maxDis), Random.Range(-maxDis, maxDis));
}
}


• This is indeed weird. Start should be called before the first Update and initialize Waypoint with a random value. Perhaps you forgot to set a value for maxDis in the inspector? But if it were 0, then the object would get stuck at the 0:0 point and not wander after reaching it. If I were you I would set a couple debugger breakpoints to find out what's actually happening to the variable Waypoint and when. Jul 23 at 10:18
• I did set the value for maxDis in the inspector but the enemy keep moving straight to 0:0 and after that it begin to wander randomly like it should be Jul 23 at 12:21
• So? What happens when you set a debugger breakpoint in Start and Update and watch the value of Waypoint? Jul 23 at 12:23
• Ok so I place the breakpoint at the waypoint and visual studio show the value is (0.0,0.0) but its should be random in the set range. I also set the maxDis to 3 Jul 23 at 12:53
• I tried the code you posted in a new project and it works as it should - the gameObject moves to a different point everytime I enter test mode. So the problem has to be somewhere outside of that script. Jul 23 at 19:36

With the code in setNewDes() you are moving the enemies towards a position that is always relative to the position of the parent x: 0, y: 0, which in your case appears to be the scene itself.
void setNewDes() {