- Home /
More Efficient way?
Hi,
I have set up a system for an enemy to detect a target within a set radius, set with a child trigger. When a target enters the child trigger, it is added to an array. When it leaves the child trigger, it is removed. The enemy then process through each entry in the array once every second or so, then finds the closest target. It works but... I'm getting dramatic slowdowns when there are more than one target. Getting ALL targets using GameObject[] runs smoothly, but it can't be resized. Is there a more efficient way to make this work? Attached are the two scripts.
The enemy script that does the finding nearest target:
The child script with the trigger that adds the targets to the Array:
It's easier if you copy the code here in the code tags, ins$$anonymous$$d of using screenshots.
You seem to use an untyped variable inside your "ChildScriptZombie". What's the type of the validTargets variable? I guess you use a Javascript "Array", right? That class is untyped and extremely slow. You should use a generic List.
There should never be any reason to not use pragma strict when using UnityScript. It just allows you to write very bad and slow code.
Also how many enemies are there? $$anonymous$$aybe you can use a singleton manager that keeps track of all enemies? So a new enemy will automatically add itself to that singleton which would use a simple generic List. Another common solution is to use Physics.OverlapSphere to get all enemies within a certain radius.
I've asked several questions which you didn't answer yet. Again:
What's the type of your "validTargets" variable?
Is there any reason why you don't use
#pragma strict
?How many enemies are in your scene?
Answer by shadowpuppet · Jul 12, 2017 at 08:40 PM
well, I don't know about more efficient but I don't seem to get slow downs with my method, only an occasional confused robot. I have a helper robot that attacks the closest enemy that is in range, if none are in range then it enables a script ( referred to in the script as "friends") but also in the script is a raycast so that if while attacking, my robot hits anything tagged "wall" he won't try to shoot through it but move closer to the enemy until that raycast hits him and he resumes shooting. I'll past the script here though it probably needs more explanation. I basically check the distance of the 6 respawning enemies from their location to the robot and the closest one occupies the open Public gameObject spot ( BadGuy) and that is his target.
hmmm. the icons to paste in code are suspiciously missing so I will have to paste it in this window
using UnityEngine;
using System.Collections;
public class enemyManager : MonoBehaviour {
public GameObject TARGET;
private NavMeshAgent agent;
public int MaxDist = 45;
public float MinDist;
Animator anim;
public static bool noEnemies;
public Light lite;
public float ro = 100f;
public float rot = 100f;
public float gat = 100f ;
public float shoot = 100f;
public float na = 100f;
public float ne = 100f;
public static Transform badGuy;
public static GameObject badGuyAgain;
public float closestEnemy;
public GameObject BadRobot;
public GameObject BadRobot2;
public GameObject GatlinGun;
public GameObject Shutze;
public GameObject Nazi;
public GameObject NeoNazi;
public GameObject PlayerAgain;
public GameObject rocketSource;
public GameObject player;
public Transform badrobo;
public Transform badrobo2;
public Transform gatgun;
public Transform shoots;
public Transform naz;
public Transform neonaz;
public Transform Player;
private MonoBehaviour friends;
private MonoBehaviour attack1;
private MonoBehaviour bullet;
private MonoBehaviour currentHealth;
private float damping = 6f;
private float TurnDist;
private int dist = 21;
private float draw = 1f;
public float off = .1f;
Ray thugRay;
RaycastHit rayHit;
public bool enemy;
void Start () {
badrobo = BadRobot.transform;
badrobo2 = BadRobot2.transform;
gatgun = GatlinGun.transform;
shoots = Shutze.transform;
naz = Nazi.transform;
neonaz = NeoNazi.transform;
bullet = rocketSource.GetComponent<myRobotBullet>();
agent.enabled = true;
}
void Awake(){
anim = GetComponent<Animator>();
agent = GetComponent<NavMeshAgent>();
currentHealth = GetComponent<myRobotHealth>();
friends = GetComponent<myRobotAlly>();
attack1 = GetComponent<myRobotAttack>();
}
void FixedUpdate () {
closestEnemy = (Mathf.Min(ro, rot, gat, shoot, na));
TARGET = badGuyAgain;
if(badGuyAgain != null)
{
if(badGuyAgain .activeInHierarchy == false)
{
friends.enabled = true;
bullet.enabled = false;
}
}
if(badGuyAgain == NeoNazi ||badGuyAgain == BadRobot || badGuyAgain == BadRobot2 || badGuyAgain ==Shutze || badGuyAgain == GatlinGun || badGuyAgain == Nazi)
{
enemy = true;
ATTACK ();
}
}
void LateUpdate()//___________________________________________________________________________________
{
if(BadRobot.activeInHierarchy == true)
{
ro = Vector3.Distance(transform.position,badrobo.position);
}
else if(BadRobot.activeInHierarchy == false)
{
ro = 100f;
}
if(NeoNazi.activeInHierarchy == true)
{
ne = Vector3.Distance(transform.position,badrobo.position);
}
else if(NeoNazi.activeInHierarchy == false)
{
ne = 100f;
}
if(BadRobot2.activeInHierarchy == true)
{
rot = Vector3.Distance(transform.position,badrobo2.position);
}
else if(BadRobot2.activeInHierarchy == false)
{
rot = 100f;
}
if (GatlinGun.activeInHierarchy == true)
{
gat = Vector3.Distance(transform.position,gatgun.position);
}
else if (GatlinGun.activeInHierarchy == false)
{
gat = 100f;
}
if (Shutze.activeInHierarchy == true)
{
shoot = Vector3.Distance(transform.position,shoots.position);
}
else if (Shutze.activeInHierarchy == false)
{
shoot = 100f;
}
if (Nazi.activeInHierarchy == true)
{
na = Vector3.Distance(transform.position,naz.position);
}
else if (Nazi.activeInHierarchy == false)
{
na = 100f;
}
if(BadRobot.activeInHierarchy == true)
{
if(Vector3.Distance(transform.position,badrobo.position) <= MaxDist)
{
if(Vector3.Distance(transform.position,badrobo.position) <= closestEnemy)
{
badGuy = badrobo;
badGuyAgain = BadRobot;
ATTACK ();
friends.enabled = false;
off = .1f;
}
}
}
else if (GatlinGun.activeInHierarchy == true)
{
if(Vector3.Distance(transform.position,gatgun.position) <= MaxDist)
{
if(Vector3.Distance(transform.position,gatgun.position) <= closestEnemy)
{
badGuy = gatgun;
badGuyAgain = GatlinGun;
ATTACK ();
friends.enabled = false;
off = .1f;
}
}
}
else if (NeoNazi.activeInHierarchy == true)
{
if(Vector3.Distance(transform.position,neonaz.position) <= MaxDist)
{
if(Vector3.Distance(transform.position,neonaz.position) <= closestEnemy)
{
badGuy = neonaz;
badGuyAgain = NeoNazi;
ATTACK ();
friends.enabled = false;
off = .1f;
}
}
}
else if(Nazi.activeInHierarchy == true)
{
if(Vector3.Distance(transform.position,naz.position) <= MaxDist)
{
if(Vector3.Distance(transform.position,naz.position) <= closestEnemy)
{
badGuy = naz;
badGuyAgain = Nazi;
ATTACK ();
friends.enabled = false;
off = .0f;
}
}
}
else if (BadRobot2.activeInHierarchy == true)
{
if(Vector3.Distance(transform.position,badrobo2.position) <= MaxDist)
{
if(Vector3.Distance(transform.position,badrobo2.position) <= closestEnemy)
{
badGuy = badrobo2;
badGuyAgain = BadRobot2;
ATTACK ();
friends.enabled = false;
off = .1f;
}
}
}
else if (Shutze.activeInHierarchy == true)
{
if(Vector3.Distance(transform.position,shoots.position) <= MaxDist)
{
if(Vector3.Distance(transform.position,shoots.position) <= closestEnemy)
{
badGuy = shoots;
badGuyAgain = Shutze;
friends.enabled = false;
off = .0f;
ATTACK ();
}
}
}
else
{
//Debug.Log ("else");
}
}
void ATTACK(){
enemy = true;
MinDist = 14;
var rotation = Quaternion.LookRotation(badGuy.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
if(badGuyAgain .activeInHierarchy == false)
{
agent.acceleration = 1.5f;
badGuy = null;
badGuyAgain = null;
TARGET = null;
enemy = false;
}
if (badGuy != null)
{
if(badGuyAgain .activeInHierarchy == true)
{
if(Vector3.Distance(transform.position,badGuy .position) <= MaxDist)
{
if(agent.enabled == true)
agent.SetDestination(badGuy .transform.position);
agent.enabled = true;
anim.SetTrigger ("walk");
anim.ResetTrigger ("attack");
anim.ResetTrigger ("idle");
anim.ResetTrigger ("idle2");
agent.acceleration = 1.5f;
lite.color = Color.yellow;
anim.speed = 2.5f;
agent.speed = 2f;
//MinDist = 21;
}
else if(Vector3.Distance(transform.position,badGuy.position) > MaxDist)
{
//friends.enabled = true;
}
if (Vector3.Distance (transform.position,badGuy .position) <= MinDist)
{
MaxDist = 45;
//MinDist = 21;
thugRay = new Ray(transform.position + transform.up * draw- transform.right * off, transform.forward*dist);
Debug.DrawRay (transform.position+ transform.up * draw - transform.right * off, transform.forward*dist, Color.blue);
if (Physics.Raycast(transform.position+ transform.up * draw - transform.right * off, transform.forward, out rayHit)){
{
if(rayHit.transform.gameObject.tag == ("wall"))
{
HITWALL();
}
else if (rayHit.transform.gameObject.tag == ("METAL"))
{
lite.color = Color.green;
agent.enabled = false;
anim.ResetTrigger ("walk");
anim.ResetTrigger ("idle");
anim.ResetTrigger ("idle2");
anim.SetTrigger ("aim");
anim.speed = 2.5f;
bullet.enabled = true;
friends.enabled = false;
//print (rayHit.collider.gameObject.name);
}
else if (rayHit.transform.gameObject.tag == ("enemy"))
{
//print (rayHit.collider.gameObject.name);
lite.color = Color.green;
agent.enabled = false;
anim.ResetTrigger ("walk");
anim.ResetTrigger ("idle");
anim.ResetTrigger ("idle2");
anim.SetTrigger ("aim");
anim.speed = 2.5f;
bullet.enabled = true;
}
}
}
}
}
}
}
void HITWALL(){
//Debug.Log ("3");
bullet.enabled = false;
MinDist = 3f;
anim.SetTrigger ("walk");
anim.ResetTrigger ("aim");
anim.ResetTrigger ("idle");
anim.ResetTrigger ("idle2");
agent.enabled = true;
lite.color = Color.cyan;
agent.SetDestination(badGuyAgain .transform.position);
thugRay = new Ray(transform.position + transform.up * draw- transform.right * off, transform.forward*dist);
Debug.DrawRay (transform.position+ transform.up * draw- transform.right * off, transform.forward*dist, Color.yellow);
if (Physics.Raycast(transform.position+ transform.up * draw- transform.right * off, transform.forward, out rayHit,dist)){
{
if(rayHit.transform.gameObject.tag == ("METAL"))
{
ATTACK ();
}
if(rayHit.transform.gameObject.tag == ("enemy"))
{
ATTACK ();
}
}
}
}
}
Your answer
Follow this Question
Related Questions
OnTriggerEnter called only one time?!! 5 Answers
array of boxes 2 Answers
Object Pool only activating one prefab 1 Answer
Building an array help 2 Answers
(No anwers? :( )How to spawn Random Pedestrians in a raduis? 2 Answers