Вопрос /del

Начинающий
Статус
Оффлайн
Регистрация
11 Авг 2021
Сообщения
22
Реакции[?]
1
Поинты[?]
0
ИИ постоянно прилипает к нулевым координатам и застревает там, не могу понять где ошибка

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);
    }
}
 
Начинающий
Статус
Оффлайн
Регистрация
11 Авг 2021
Сообщения
22
Реакции[?]
1
Поинты[?]
0
Чуть-чуть исправил код, но все равно не работает, даже хз где у меня там ошибка. Почему-то все хорошо работает когда игрок находится на нулевых координатах, но только как ты ставишь его например на (1,1) то ИИ начинает застревать на координатах близких к нулю и дергается как на видео
C#:
using UnityEngine;
using System;
using UnityEngine.UIElements;
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);
    }
}
 

Вложения

Похожие темы
Сверху Снизу