Начинающий
-
Автор темы
- #1
ИИ постоянно прилипает к нулевым координатам и застревает там, не могу понять где ошибка
C#:
using UnityEngine;
using System;
using Random = UnityEngine.Random;
public class AIEnemy : MonoBehaviour
{
public GameObject player;
public GameObject enemyPrefab;
[Space]
public float speed = 5f;
public float Radius = 35f;
private Vector3 enemyPos = new Vector3();
private GameObject enemyClone;
void Start()
{
enemyPos = GetRandomPointInsideRadius();
enemyClone = Instantiate(enemyPrefab, enemyPos, player.transform.rotation);
}
void Update()
{
if (Math.Round(enemyClone.transform.position.x, 1) == Math.Round(player.transform.position.x, 1))
{
float dist = GetPos(player, enemyClone);
if (Math.Round(dist, 1) != Math.Round(Math.Abs(player.transform.position.x + 7f), 1))
{
if (dist < Math.Abs(player.transform.position.x + 7f))
{
if (enemyClone.transform.position.y < 0f)
{
Moved(180f); //Вниз
}
else if (enemyClone.transform.position.y > 0f)
{
Moved(0f); //Вверх
}
}
else if (dist > Math.Abs(player.transform.position.x + 7f))
{
if (enemyClone.transform.position.y < 0f)
{
Moved(0f); //Вверх
}
else if (enemyClone.transform.position.y > 0f)
{
Moved(180f); //Вниз
}
}
}
}
if (Math.Round(enemyClone.transform.position.y, 1) == Math.Round(player.transform.position.y, 1))
{
float dist = GetPos(player, enemyClone);
if (Math.Round(dist, 1) != Math.Round(Math.Abs(player.transform.position.y + 7f), 1))
{
if (dist < Math.Abs(player.transform.position.y + 7f))
{
if (enemyClone.transform.position.x < 0f)
{
Moved(90f); //Влево
}
else if (enemyClone.transform.position.x > 0f)
{
Moved(270f); //Вправо
}
}
else if (dist > Math.Abs(player.transform.position.y + 7f))
{
if (enemyClone.transform.position.x < 0f)
{
Moved(270f); //Вправо
}
else if (enemyClone.transform.position.x > 0f)
{
Moved(90f); //Влево
}
}
}
}
if (Math.Round(enemyClone.transform.position.y, 1) != Math.Round(player.transform.position.y, 1) &&
Math.Round(enemyClone.transform.position.x, 1) != Math.Round(player.transform.position.x, 1))
{
if (Math.Abs(enemyClone.transform.position.y - player.transform.position.y) >
Math.Abs(enemyClone.transform.position.x - player.transform.position.x))
{
if (enemyClone.transform.position.x < 0f)
{
Moved(270f); //Вправо
}
else if (enemyClone.transform.position.x > 0f)
{
Moved(90f); //Влево
}
}
else if (Math.Abs(enemyClone.transform.position.y - player.transform.position.y) <
Math.Abs(enemyClone.transform.position.x - player.transform.position.x))
{
if (enemyClone.transform.position.y < 0f)
{
Moved(0f); //Вверх
}
else if (enemyClone.transform.position.y > 0f)
{
Moved(180f); //Вниз
}
}
if (Math.Round(player.transform.position.x, 1) != 0f &&
Math.Round(enemyClone.transform.position.x) == 0f)
{
if (enemyClone.transform.position.x < 0f)
{
Moved(270f); //Вправо
}
else if (enemyClone.transform.position.x > 0f)
{
Moved(90f); //Влево
}
}
if (Math.Round(player.transform.position.y, 1) != 0f &&
Math.Round(enemyClone.transform.position.y) == 0f)
{
if (enemyClone.transform.position.y < 0f)
{
Moved(0f); //Вверх
}
else if (enemyClone.transform.position.y > 0f)
{
Moved(180f); //Вниз
}
}
}
if (Math.Round(enemyClone.transform.position.y, 1) == Math.Round(player.transform.position.y, 1) &&
Math.Round(enemyClone.transform.position.x, 1) == Math.Round(player.transform.position.x, 1))
{
Destroy(enemyClone);
enemyPos = GetRandomPointInsideRadius();
enemyClone = Instantiate(enemyPrefab, enemyPos, player.transform.rotation);
}
}
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(GameObject a, GameObject b)
{
float result = (float)Math.Sqrt(Math.Pow(a.transform.position.x - b.transform.position.x, 2) + Math.Pow(a.transform.position.y - b.transform.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);
}
}