using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public float speed;
public float stoppingdistance;
public float retreatDistance;
private float timeBtwShots;
public float startTimeBtwShoots;
public GameObject projectile;
public Transform player;
// Start is called before the first frame update
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
timeBtwShots = startTimeBtwShoots;
}
// Update is called once per frame
void Update()
{
if (Vector2.Distance(transform.position,player.position) > stoppingdistance)
{
transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
}
else if (Vector2.Distance(transform.position, player.position) < stoppingdistance && (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);
}
if (timeBtwShots <=0)
{
Instantiate(projectile, player.position, Quaternion.identity);
timeBtwShots = startTimeBtwShoots;
}
else
{
timeBtwShots -= Time.deltaTime;
}
}
}
else if (Vector2.Distance(transform.position, player.position) < stoppingdistance && (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);
}
float dist=Vector2.Distance(transform.position,player.position);
if(dist>stoppingdistance)transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
else if(dist<retreatDistance) transform.position = Vector2.MoveTowards(transform.position, player.position*-Vector2.one, speed * Time.deltaTime); //тут надо смотреть на игра сбоку или вид с верху