I dont know how to fix the time it takes to move from the current position to the target position(wp). It doesnt feel clean and is annoyingly fast if the distance is too small.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
//move
public GameObject player;
public float speed = 20;
float WPradius = 1;
private Vector2 wp;
void Update()
{
if (Vector2.Distance(wp, transform.position) < WPradius)
{
wp = new Vector2(player.transform.position.x, UnityEngine.Random.Range(3f, -3f));
//do stuff
}
transform.position = Vector2.MoveTowards(transform.position, wp, Time.deltaTime * speed);
}
}