- Home /
Question by
Charlie010903 · Apr 13, 2020 at 03:22 PM ·
movementprefabspathfindingenemy ai
How do I get my prefab enemy to attack my moving player as I can't assign my player in the scene to my transform???
When the enemy spawns in they move towards the original position of my player (before it moved) as this is the position that the player prefab is set. How can I get the enemy to move towards the player in the scene rather than the prefab. This is the code i'm using:
void Update()
{
transform.right = player.position - transform.position;
transform.position = Vector3.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
}
Comment
Answer by JackhammerGaming · Apr 14, 2020 at 07:38 AM
@Charlie010903 it has pretty easy solution just tag your charcetr something like "Player" and following code will help
public float speed;
Transform Charcter_Transform;
void Update(){
CharcterTransform = GameObject.FindWithTag("Player").GetComponent<Transform> ();
transform.position = Vector3.MoveTowards(transform.position , CharcterTransform.Position , speed * Time.deltaTime);
}
Your answer
Follow this Question
Related Questions
Adding Wander option to AI 1 Answer
Simple 2D Enemy AI 3 Answers
How to I make FindGameObjectWithTag() not just find itself? 1 Answer
Have enemy move in 8 way direction 3D 0 Answers
Making the FPS asset move 1 Answer