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 ForceX · Mar 03, 2011 at 03:48 AM · arraydistanceresizesort

Resize Array based on distance.

I've been attempting to create a targeting system that will only allow cycling through the gameObjects if they are in range. So far i can initialize the array and populate it with all gameObjects with a tag, but then i want to remove any gameObjects that are not with in a certain distance preventing the player from selecting them as a target until they are in range.

I've tried to hack it together to make it work with:

private var Master : GameObject; private var Player : Transform; var SelectedTarget : Transform; var TargetList : GameObject[]; var currentTarget : int = 0; var RadarDistance : float;

function Awake(){ Player = transform.Find("/Player"); Master = GameObject.Find("_Master"); RadarDistance = Master.GetComponent(Radar_Master).RadarRadiusRange; SelectedTarget = null; }

function Update(){ TargetList = GameObject.FindGameObjectsWithTag("Active"); }

function FindNextTarget(){ if(SelectedTarget == null){ currentTarget = (currentTarget + 1) % TargetList.length; SelectedTarget = TargetList[currentTarget].transform; }else{ if((SelectedTarget.position - Player.position).magnitude > RadarDistance){ AdvanceCurrentTarget(); }else{ currentTarget = (currentTarget + 1) % TargetList.length; SelectedTarget = TargetList[currentTarget].transform; }}}

function AdvanceCurrentTarget(){ currentTarget = (currentTarget + 1) % TargetList.length; SelectedTarget = TargetList[currentTarget].transform; FindNextTarget(); }

This works but it skips the first gameobject in the array that is in range. It does this because FindNExtTarget() will cycle through until it finds a target that is in range and +1. Effectively skipping the first target. I know there is a correct way of doing this, but i'm not sure how.

I was thinking of trying this but i dont know how to get it back into a new array.

TargetList = GameObject.FindGameObjectsWithTag("Active");

for (var t GameObject in TargetList){ var dist = (Player.position - t.transform.position).magnitude; if(dist < RadarDistance){ //Add gameobjects under RadarDistance to new array. } }

Any help is appreciated.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Alec-Slayden · Mar 03, 2011 at 04:56 AM

if you can spare the resources, I would probably use a separate hashtable myself for its utility, and for storing information about the target for use later.

var validTargets = new Hashtable();

For the distance check, you can use

for (var t GameObject in TargetList){ var dist = (Player.position - t.transform.position).magnitude; if(dist < RadarDistance && !validTargets.ContainsKey(t)){ validTargets.Add(t, "target info"); } if(dist >= RadarDistance && validTargets.ContainsKey(t)){ validTargets.Remove(t); }

 }

with a hash table you can grab the object name as a string, or some odd variables in a script of the target for use in your HUD or whatever /shrug. The issue with hashtables is that the keys aren't easily sorted, so this will require more effort in terms of your selection script grabbing a different key. It'd have to be a grab of the next from the target list and then that translated into the hash key.

Avoiding all the hash hubbub:

var validTargets = new Array();

validTargets.Clear(); for (var t GameObject in TargetList){ var dist = (Player.position - t.transform.position).magnitude;
if(dist < RadarDistance){ validTargets.Add(t); }
}

Then you cycle through the validTargets instead of the TargetList. This is clearing and re-creating a javascript array every time, but is easier to script. If fast scripts are critical, it may be best to use built-in arrays instead, maybe make validTargets a duplicate of TargetList, where the index of invalid targets is set to null, and the index of valid targets has the object.

Comment
Add comment · Show 2 · 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
avatar image ForceX · Mar 03, 2011 at 12:43 PM 0
Share

This is great. Thanks for the responses. When i get home from work i'm going to give these ideas a try and then i'll let you know how it worked out.

avatar image ForceX · Mar 04, 2011 at 12:04 AM 0
Share

I used your second method with great success. Thank you for sharing this with me.

avatar image
1
Best Answer

Answer by by0log1c · Mar 03, 2011 at 04:57 AM

I started to write the full code because I feel your example is a over-complicating things a little but your second idea seems quite right. Can't you just call the below example when you need it? Then iterate through the result and pick whichever you prefer?

function FindTarget(){ TargetList = GameObject.FindGameObjectsWithTag("Active");

for(var t GameObject in TargetList){
    var sqrLen = Vector3.sqrMagnitude(Player.position - t.transform.position);
    if(sqrLen < RadarDistance*RadarDistance){
        //Debug.Log(t.name+" is nearby! To arms!\n")
    }
}

}

What's the issue with "a new array"? Can we just overwrite the old one?

We could also have an array holding all enemies and a second array holding boolean for each enemies and simply turn the boolean on/off accordingly. That might ends up much cheaper cpu-speaking if you're to call FindTarget() often.

Comment
Add comment · Show 2 · 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
avatar image ForceX · Mar 03, 2011 at 09:33 PM 0
Share

Thank you for your suggestion. While i'm using the posters advice from above i am curious as to the boolean method. Would i need to create a var inside my target to include a "InRange : boolean" and then search to to see if it is true before adding it to the array? Like..for (var t : GameObject in TargetList){ t.getcomponent(script).Inrange; if(t.Inrange == true) //Add;}

avatar image by0log1c · Mar 03, 2011 at 10:31 PM 0
Share

$$anonymous$$y pleasure. Well I'd do just that. Except that i'd have the booleans array inside the same script, rather than iterating through all the enemies scripts.

This way allow you to make a single .Find and even to use built-in array, which are faster than Hashtables or JS array. That's the way I'd do it,anyway :)

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

No one has followed this question yet.

Related Questions

Find Nearest Object from an Array of Transforms 1 Answer

C# copy Sorting data of An Array to Another Array 1 Answer

Error message when accessing class array 1 Answer

geting the closest object from a array 2 Answers

Sort By Distance, Call it multiple times. 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