Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Therian13 · Jun 22, 2015 at 09:51 AM · javascripttransformarraytargetfor-loop

have array target person of most priority/stick with top prior

Hello Everyone.

I am currently making a script that allows it to detect players and their ships, and then target them according to priority number. (the lower the number, the more important it is).

I have a somewhat functioning one, but it keeps rapidly changing between targets, as well as being unable to focus on the top target. (it targets him for a second before targeting a less important Transform again).

It is at the FindTarget function.

would someone be nice enough to take a look and let me know where I messed up at? Thank you for your time!

  public var fieldOfViewAngle : float = 110f; 
 var sharkMain : scoutshipAI;
 private var col : SphereCollider;  
 private var search : boolean = true;
 var targets : Transform[];
 private var empty : Transform;
 var tarNum : int = 999;
 var newTarget : boolean = false;
 
 
 function Awake()
 {
 col = GetComponent(SphereCollider);//needed to detect character in field of view
 }
 
 
 function Update () 
 {
         if (targets.Length == 0)
         {
         var newContents = new Array(targets);
         newContents.Add(empty);
         targets=newContents.ToBuiltin(Transform); //Array to unity builtin array
         }
         if (search == true)
         {
         findTarget();
         }
 
 }
 
 function OnTriggerEnter(other : Collider)
 {
     if(other.tag == "Player" || other.tag == "ship" ||other.tag == "Bullet") //make sure target is player or other "threat"
     {
         for(var i : int = 0; i < targets.Length; i++)
         {
             if (other.transform != targets[i])
             {
                 if (targets[0] == null) // assign target to blank spot;
                 {
                 targets[0] = other.transform;
                 var newContents = new Array(targets);
                 targets=newContents.ToBuiltin(Transform); //Array to unity builtin array
                 }
     else //add in new spot for new target
     {
     newContents = new Array(targets);
     newContents.Add(other.transform);
     targets=newContents.ToBuiltin(Transform); //Array to unity builtin array
     }
             }
             for(var j : int = 0; j < targets.Length-1; j++) //needed to check for extra copies on the target list.
             {
                 //compares two different list spots and see if they are the same. if so, delete the extra.
                 if (targets[i] == targets[j]) 
                 {
                     if (j != i) ///make sure it's not comparing the same list spot.
                     {
                     newContents = new Array(targets);
                     newContents.RemoveAt(i);
                     targets=newContents.ToBuiltin(Transform); //Array to unity builtin array
                     }
                 }
             }
         }
     }
 }
 
 function findTarget()
 {
 search = false;
     for(var i : int = 0; i < targets.Length; i++)
     {
     //maybe don't need this here. move to J maybe?
         if (targets[i] != null) //we have at least one target in the array
         {
             for(var j : int = 1; j < targets.Length; j++) //needed to check for extra copies on the target list.
             {    
             //chooses between two or more targets with same priority level, and make that the target
                 if (targets[i] != targets[j]) 
                 {
                     if (j >= i) 
                     {
                     Debug.Log(tarNum);
                     //needs more work. keeps cycling through despite priority. (targets player for a second before target other one again);
                         if (tarNum > (targets[j].GetComponent(priorityScript).priorityNumber))
                         {
                         Debug.Log("greater than tarnum");
                         tarNum = targets[j].GetComponent(priorityScript).priorityNumber;
                         sharkMain.Player = targets[j];
                         sharkMain.provoked = true;
                         yield WaitForSeconds(1);         
                         }
                         
                         else if (tarNum < (targets[j].GetComponent(priorityScript).priorityNumber))
                         {
                         Debug.Log("lesser than tarnum");
                         tarNum = targets[i].GetComponent(priorityScript).priorityNumber;
                         sharkMain.Player = targets[i];
                         sharkMain.provoked = true;
                         yield WaitForSeconds(1);     
                         }
                         
                         
                         else if (tarNum == targets[j].GetComponent(priorityScript).priorityNumber)
                         {
                             if (newTarget == false)
                             {
                             newTarget = true;
                             choose = Random.Range(0, targets.Length); 
                             Debug.Log("equal to tarnum");
                             Debug.Log("tarNum range is from " + i + " to " + j);
                             tarNum = targets[choose].GetComponent(priorityScript).priorityNumber;
                             sharkMain.Player = targets[choose];
                             sharkMain.provoked = true;
                             yield WaitForSeconds(1); 
                             }
                         }
                     }
                 }
             }
          }
     }
     yield WaitForSeconds(5);
      search = true;
      newTarget = false;
 }
 
 function OnTriggerExit (other : Collider)
 {
     if(other.tag == "Player" || other.tag == "ship" ||other.tag == "Bullet") //make sure target is player or other "threat"
     {
         for(var i : int = 0; i < targets.Length; i++)
         {
             if (other.transform == targets[i])
             {
             var newContents = new Array(targets);
             newContents.RemoveAt(i);
             targets=newContents.ToBuiltin(Transform); //Array to unity builtin array
             }
            }
     }
     if (sharkMain.Player != null)
     {
         if (other.tag == sharkMain.Player.tag)
         {
         sharkMain.Player = null;
         sharkMain.provoked = false;
         }
     }
 
 }
                             
                         
Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

21 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

Related Questions

What is wrong with this code? 0 Answers

Instantiating from an object that's in an array 1 Answer

Turn all objects in the array to make same thing 0 Answers

How do I create an array for multiple targets? 1 Answer

(20,57): BCE0022: Cannot convert 'EnemyAI[]' to 'EnemyAI'. 1 Answer


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