@keksmr
Unity разработчик

Unity почему из за моих ботов происходят просадки Fps?

есть скрипт для ботов типа clash of clans
Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class Crip : MonoBehaviour
{


    #region Varibales

    #region Settings
    public bool isBOT;
    public bool isMyTeam;
  
    private NavMeshAgent agent;

    public Transform Target;

    public Transform enemy;

    float range = 5f;
    [Header("Attack Settings")]
    public float Health;
    public float Hit;

    [Header("Cooldown")]
    public float currCoolDown, Cooldown;

    [Space]
    #endregion


    #region Distance
    [Header("Distance Settings")]
    public float attackDist;
    public float agentDist;
    public float MaxDist;
    #endregion

    #region Prior
    public enum Priority
    {
        Baza,
        Enemy
    }

    [Space]

    [Header("Priority")]
    public Priority prior;
    #endregion

    #endregion
    // Start is called before the first frame update
    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        if (!isBOT)
            Target = GameObject.Find("ENEMYHOUSE").transform;
        else
        {
            Target = GameObject.Find("MYHOUSE").transform;
            isMyTeam = false;
        }
    }

    

    void Shoot()
    {
        Debug.Log("2");
        if (!CanShoot())
            return;
        currCoolDown = Cooldown;
        agent.SetDestination(enemy.transform.position);
        enemy.GetComponent<Crip>().Health -= Hit;
        Debug.Log("attack!");
    }

    bool CanShoot()
    {
        if (currCoolDown <= 0)
            return true;
        return false;
    }

    // Update is called once per frame
    void Update()
    {
        if (Health == 0)
        {
            Debug.Log("Die " + gameObject.name);
            Destroy(gameObject);
        }
        if(enemy != null)
        {
            float dist = Vector3.Distance(transform.position, enemy.position);
            if (prior == Priority.Enemy)
            {
                if (dist <= attackDist)
                {
                    Shoot();
                }
                else if (dist > attackDist && dist <= agentDist)
                    agent.SetDestination(enemy.transform.position);
                else if (dist > agentDist && dist >= MaxDist)
                    enemy = null;
            }
            else if (prior == Priority.Baza)
            {
                if (dist <= attackDist)
                {
                    Shoot();
                    agent.SetDestination(enemy.transform.position);
                }
                else
                    agent.SetDestination(Target.transform.position);
            }

        }
        else
            agent.SetDestination(Target.transform.position);

        if (currCoolDown > 0)
            currCoolDown -= Time.deltaTime;
        
    }

    private void OnTriggerEnter(Collider other)
    {
        Debug.Log("StartOntTrigger");
        if (enemy != null)
            return;


        if (other.gameObject.GetComponent<Crip>().isMyTeam != gameObject.GetComponent<Crip>().isMyTeam)
            enemy = other.transform;
        Debug.Log("1");
    }
}


При 40 кубах на которых весит этот скрипт происходят просадки до 10 фпс почему?
  • Вопрос задан
  • 88 просмотров
Решения вопроса 1
GavriKos
@GavriKos Куратор тега Unity
Потому что написали где то фигню.

По факту - открываете Profiler и смотрите. Если непонятно - расставляете beginsample/endsample и смотрите детальнее.

Навскидку - могут тормозить логи, могут тормозить просчеты расстояний, может тормозить постоянный SetDestionation.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы