S
S
Sergey Ivanov2019-06-21 10:19:50
Unity
Sergey Ivanov, 2019-06-21 10:19:50

Unity why are my bots causing Fps drops?

there is a script for bots like 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");
    }
}

With 40 cubes on which this script weighs, there are drawdowns of up to 10 fps, why?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2019-06-21
@keksmr

Because they wrote garbage somewhere.
In fact - open Profiler and see. If it's not clear, arrange beginsample/endsample and look in more detail.
Offhand - logs can slow down, distance calculations can slow down, constant SetDestionation can slow down.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question