- Home /
Why do the enemies not follow me?
using UnityEngine;
using System.Collections;
public class EAI : MonoBehaviour {
public GameObject targetPos;
public float speed = 2f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float step = speed * Time.deltaTime;
//transform.position = Vector3.MoveTowards(targetPos.gameObject.transform.position.x, targetPos.gameObject.transform.position.y, step);
transform.position = Vector3.MoveTowards (transform.position, targetPos.gameObject.transform.position, step);
}
}
For speed i have 1 (They are moving, just towards the center of the screen) and for targetPos i have set the play ship.
Debug.Log(transform.position+" "+targetPos.gameObject.transform.position+" "+step);
Answer by wesleywh · Jul 14, 2015 at 06:43 PM
Well if that code doesn't work then try this other code out:
this.transform.position = Vector3.Lerp(transform.position, targetPos.transform.position, step);
To have it follow a prefab you can't just drag your prefab into the inspector since that doesn't work, sad day I know. To get around this problem you will have to find your prefab as soon as it enters the game by its TAG or NAME. Here is some example code of how I might find the prefab by its tag:
void Update () {
if(FindTheTarget("Player") != null){
float step = speed * Time.deltaTime;
targetPos = FindTheClosestTarget("Player");
transform.position = Vector3.MoveTowards (transform.position, targetPos.transform.position, step);
}
}
GameObject FindTheTarget(TagToSearch:String){
GameObject target;
target = GameObject.FindGameObjectWithTag(TagToSearch);
return target;
}
This is counting on the fact that there is only going to be one player. If you want to have more than one player to check for then you will need to make target an array and loop through that array for the closest one.(That is if you wanted to find the closest target).
Let me know how this goes for you.
An alternative way of writing this would be the following:
void Update () {
GameObject target = GameObject.FindGameObjectWithTag("Player");
if(target != null){
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards (transform.position, target.transform.position, step);
}
}
No its not static, and changing the code didnt help, sorry :( but thanks!
No sorry this just makes them move towards the center of the screen quicker, the center of the screen is where the player starts, so the position of the player must not be updating?
Yes, either you don't update your characters position or you are watching the wrong character. Try dragging your object back into the targetPos slot in the inspector again just to make sure you didn't put the wrong character to watch there.
I have the correct character, but idk whether i should select the character from in game, the on in the hierarchy, or the prefab? As it does not let me select the hierarchy one!
Your answer

Follow this Question
Related Questions
Ways to find bounds of the platform 1 Answer
MoveTowards... a smoother way? 2 Answers
How do I implement A* pathfinding to my 2d game, without tiles? 4 Answers
some piston "ai"? 1 Answer
conserve momentum of vector3.moveTowards in 2D game 0 Answers