Answer the question
In order to leave comments, you need to log in
Unity why are my bots causing Fps drops?
there is a script for bots like clash of clans
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");
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question