- Home /
enemy folow whats wrong with it
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Enemy : MonoBehaviour {
public float speed; public float stoopingDistance; public float retreatDistance;
public Transform player;
void Start() {
player = GameObject.FindGameObjectWithTag("Player");
}
void Update() {
if(Vector2.Distance(transform.position, player.position) > stoopingDistance){
transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
} else if(Vector2.Distance(transform.position, player.position) < stoopingDistance && Vector2.Distance(transform.position, player.position) > retreatDistance){
transform.position = this.transform.position;
} else if(Vector2.Distance(transform.position, player.position) < retreatDistance){
transform.position = Vector2.MoveTowards(transform.position, player.position, -speed * Time.deltaTime);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Help with Enemy AI 1 Answer
How to make a follow AI 4 Answers
Why does my AI miss me every few feet? 1 Answer
Follow Player Via Waypoints 1 Answer
How To Make Enemy Move Around 0 Answers