0
\$\begingroup\$

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));
    }
}

\$\endgroup\$
6
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Philipp
    Jul 23 '21 at 10:18
  • \$\begingroup\$ 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 \$\endgroup\$
    – Alynred
    Jul 23 '21 at 12:21
  • \$\begingroup\$ So? What happens when you set a debugger breakpoint in Start and Update and watch the value of Waypoint? \$\endgroup\$
    – Philipp
    Jul 23 '21 at 12:23
  • \$\begingroup\$ 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 \$\endgroup\$
    – Alynred
    Jul 23 '21 at 12:53
  • \$\begingroup\$ 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. \$\endgroup\$
    – Philipp
    Jul 23 '21 at 19:36
0
\$\begingroup\$

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.

By adding the new position to the current position, the new calculation destination would be relative to the current position:

void setNewDes() {
    Waypoint = new Vector2(
        transform.position.x + Random.Range(-maxDistance, maxDistance),
        transform.position.y + Random.Range(-maxDistance, maxDistance)
    );
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .