Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by Xplodingducks · Jul 12, 2017 at 06:28 PM · javascriptscripting problemarrayai

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:

alt text

The child script with the trigger that adds the targets to the Array: alt text

screenshot-2017-07-12-111441.png (155.6 kB)
screenshot-2017-07-12-111510.png (187.8 kB)
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShadyProductions · Jul 12, 2017 at 08:07 PM 1
Share

It's easier if you copy the code here in the code tags, ins$$anonymous$$d of using screenshots.

avatar image Bunny83 · Jul 12, 2017 at 08:48 PM 0
Share

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.

avatar image minime15v Bunny83 · Jul 12, 2017 at 09:03 PM 0
Share

@Bunny83 please help? T.T

avatar image Bunny83 minime15v · Jul 13, 2017 at 12:07 AM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

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 ();
                 }
             }
         }
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

180 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges