- Home /
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 it, try to do step-by-step:
Find the distance between the enemy and the player position.
Try to rotate the enemy to face the player, it's a nice detail.
Don't forget to assign a Rigidbody to the enemy in order to move it
See if you can work it out!
Answer by UnityToMakeMoney · May 11, 2020 at 12:02 AM
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.
The only problem is that all the enemy's clump together into one big enemy any advice
@Stactic_Games You can make less enemies which will $$anonymous$$imize clumping.
as someone else suggested, you can make fewer enemies, but alternatively, you can use this exact same script, but now it would be for enemies so that they aren't together as much. You can also set a limit of enemies spawned at a time by counting how many have been spawned and destroyed.
what do you mean? Are they not prefabs? Can you please elaborate?
You can add rigidbody and colliders to prevent the enemies from stacking on top of eachother
Or maybe you could try A* path finding algorithm for this.
check it out : youtube link
Answer by unity_414apache · May 11, 2020 at 08:24 AM
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 !
Answer by lucasdvazquez7 · May 11, 2020 at 04:14 AM
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!
Your answer
Follow this Question
Related Questions
Help with Enemy AI 1 Answer
How To Make Enemy Move Around 0 Answers
Problem with a monster (he follows me backwards) 1 Answer
Enemy Pathing 1 Answer
Following AI - Similar to Snake Game 2 Answers