ИИ застревает на нулевых координатах, даже хз где тут может быть ошибка. Почему-то все хорошо работает когда игрок находится на нулевых координатах, но только как ты ставишь его например на (1,1) то ИИ начинает застревать на координатах близких к нулю и дергается.
using UnityEngine;
using System;
using Random = UnityEngine.Random;
public class AIEnemy : MonoBehaviour
{
public Transform player;
public GameObject enemyPrefab;
[Space]
public float speed = 5f;
public float Radius = 35f;
private Transform enemyClone;
void Start()
{
Vector3 enemyPos = GetRandomPointInsideRadius();
enemyClone = Instantiate(enemyPrefab, enemyPos, player.transform.rotation).transform;
}
void Update()
{
if (Math.Round(enemyClone.position.x, 1) == Math.Round(player.position.x, 1) &&
Math.Round(enemyClone.position.y, 1) != Math.Round(player.position.y, 1))
{
float dist = GetPos(player, enemyClone);
if (Math.Round(dist, 1) != Math.Round(Math.Abs(player.position.x + 7f), 1))
{
if (dist < Math.Abs(player.position.x + 7f))
{
if (enemyClone.position.y < 0f)
{
Moved(180f); //Вниз
}
else if (enemyClone.position.y >= 0f)
{
Moved(0f); //Вверх
}
}
else if (dist >= Math.Abs(player.position.x + 7f))
{
if (enemyClone.position.y < 0f)
{
Moved(0f); //Вверх
}
else if (enemyClone.position.y >= 0f)
{
Moved(180f); //Вниз
}
}
}
}
if (Math.Round(enemyClone.position.y, 1) == Math.Round(player.position.y, 1) &&
Math.Round(enemyClone.position.x, 1) != Math.Round(player.position.x, 1))
{
float dist = GetPos(player, enemyClone);
if (Math.Round(dist, 1) != Math.Round(Math.Abs(player.position.y + 7f), 1))
{
if (dist < Math.Abs(player.position.y + 7f))
{
if (enemyClone.position.x < 0f)
{
Moved(90f); //Влево
}
else if (enemyClone.position.x >= 0f)
{
Moved(270f); //Вправо
}
}
else if (dist >= Math.Abs(player.position.y + 7f))
{
if (enemyClone.position.x < 0f)
{
Moved(270f); //Вправо
}
else if (enemyClone.position.x >= 0f)
{
Moved(90f); //Влево
}
}
}
}
if (Math.Round(enemyClone.position.y, 1) != Math.Round(player.position.y, 1) &&
Math.Round(enemyClone.position.x, 1) != Math.Round(player.position.x, 1))
{
if (Math.Abs(enemyClone.position.y - player.position.y) >
Math.Abs(enemyClone.position.x - player.position.x))
{
if (enemyClone.position.x < 0f)
{
Moved(270f); //Вправо
}
else if (enemyClone.position.x >= 0f)
{
Moved(90f); //Влево
}
}
else if (Math.Abs(enemyClone.position.y - player.position.y) <=
Math.Abs(enemyClone.position.x - player.position.x))
{
if (enemyClone.position.y < 0f)
{
Moved(0f); //Вверх
}
else if (enemyClone.position.y >= 0f)
{
Moved(180f); //Вниз
}
}
}
if (Math.Round(enemyClone.position.y, 1) == Math.Round(player.position.y, 1) &&
Math.Round(enemyClone.position.x, 1) == Math.Round(player.position.x, 1))
{
}
}
private Vector3 GetRandomPointInsideRadius()
{
Vector3 randDirection2d = Random.insideUnitCircle.normalized;
float randDist = Random.value * Radius;
Vector3 randPoint = randDirection2d * randDist;
return player.transform.position + randPoint;
}
private float GetPos(Transform a, Transform b)
{
float result = (float)Math.Sqrt(Math.Pow(a.position.x - b.position.x, 2) + Math.Pow(a.position.y - b.position.y, 2));
return result;
}
void Moved(float z)
{
enemyClone.transform.rotation = Quaternion.Euler(0f, 0f, z);
enemyClone.transform.Translate(Vector3.up * speed * Time.deltaTime);
}
}