How do I make a enemy follow me

I am making a fps shooter and I need my enemy to follow me when I am with in a certain range of the enemy this is what I have so far.`
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovetowrdsPlayer : MonoBehaviour
{
    public GameObject Player;
    public bool Speed;
    public void Update()
    {
        rigidbody.velocity = transform.forward * Speed;
    }
}

is this really the way

You can do this in a variety of ways.

One method I recently used was adding a Sphere Collider to the enemy, with a big radius. Then, I created a script that tells the enemy to follow the player if the player collides with the sphere collider.

public class EnemyFollow : MonoBehaviour
{
	//I first start getting the transform (position) of my player
        public Transform target;
        //Then I set up the speed of the enemy, that I can edit later
	public float speed = 2f;
        //Lastly, I added the enemy a rigidbody
	public Rigidbody rb;

//First thing, I will create a function that follows the player
void FollowPlayer(){
    	//I will create a vector 3 called pos that stores the movement that I want my player to do
         Vector3 pos = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
        //I will use these two built-in functions to follow the player
        rb.MovePosition(pos);
       	transform.LookAt(target);
    }
    
//Finally, I add a collider function that calls the FollowPlayer() function when it is within its range
void OnTriggerStay(Collider player){
	  		if(player.tag == "Player"){
	    		FollowPlayer();
	    	}
	    }
}

There are many other ways, but I think this is the simplest one and will work for you. Hope this helps!

using UnityEngine;
using System.Collections;

//[RequireComponent(typeof(CharacterController))]

public class Chaser : MonoBehaviour {

public float speed = 20.0f;
public float minDist = 1f;
public Transform target;

// Use this for initialization
void Start () 
{
	// if no target specified, assume the player
	if (target == null) {

		if (GameObject.FindWithTag ("Player")!=null)
		{
			target = GameObject.FindWithTag ("Player").GetComponent<Transform>();
		}
	}
}

// Update is called once per frame
void Update () 
{
	if (target == null)
		return;

	// face the target
	transform.LookAt(target);

	//get the distance between the chaser and the target
	float distance = Vector3.Distance(transform.position,target.position);

	//so long as the chaser is farther away than the minimum distance, move towards it at rate speed.
	if(distance > minDist)	
		transform.position += transform.forward * speed * Time.deltaTime;	
}

// Set the target of the chaser
public void SetTarget(Transform newTarget)
{
	target = newTarget;
}

}
@ Static_Games , this is the basic chaser script. Hope this helps !

Here is a script that might help

//set the values in the inspector
public Transform target; //drag and stop player object in the inspector
public float within_range;
public float speed;

public void Update(){
     //get the distance between the player and enemy (this object)
    float dist = Vector3.Distance(target.position, transform.position);
    //check if it is within the range you set
    if(dist <= within_range){
      //move to target(player) 
      transform.position = Vector3.MoveTowards(transform.position, target.transform.position, speed);      
    }
    //else, if it is not in rage, it will not follow player
}

The way you were trying to accomplish wouldn’t work, but I see what you were going for.

I get weird behavior using this. If I restart the scene a couple of times the enemies fly away from the player. It usually happens after 4th restart, sometimes more. @unity_414apache