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 superventure · Apr 25, 2015 at 11:49 AM · arrayorder

More of a Logic Qe for ordering an array

Using for(var i = 0; i < foo.length; i++)

I'm trying to get a 'following' concept set up to where a random number of objects (npcs) 1 to 3 , follows a specific transform point based off of the greatness of a stat they posses which could be unique or the same to each other. The points are the Main player transform, and two other empty game objects transforms i call leadLeft and leadRight, nested respectively within the player. The npcs are of different heights, the 'stat' is their own height level. I would like to have the shortest of any of these npcs follow leadLeft, the tallest to leadRight. Anyone who is taller than the shortest yet smaller then the highest will follow the mainPlayer transform which is between leadLeft and leadRight.

if there are 2 npcs out, they follow at the left or right with the same concept. if only one is out it will follow the player transform.

The code is simple enough at this point- My issue is that the npcs are picked at will, and there can be (duplicate) npcs who have the same height.

I need help trying to figure out how to state the 'if' statements for the best way to compare each npc in the array to handle any duplicate heights and still order them to follow the transform points from least to greatest to each other if one of the three is taller or shorter, or if all 3 are of equal heights.

For example, lets say npc[0]= 2, npc[1] = 7, and npc[3] = 2.

npc[0] would be assigned leadLeft npc[1] would be leadRight npc[2] would be the mainPlayer

If all three are equal they could be assigned to the leads in order to their place in the array.

I am confused how to state the logic at this point.

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 siaran · Apr 25, 2015 at 01:39 PM 0
Share

If I understand correctly, you need to 'separate' your arrays in 3 different arrays: Highest NPCs, Lowest NPCs and InBetween NPC's?

I suspect the easiest way to do this would be to get the highest and lowest values that exist in the array, then loop through it again and assign the elements to their proper array.

I'd do something like

 using System.Collections.Generic;
 //the array with the npcs
 public NPC[] npcs;
 
 //lists that contain npcs
 List<NPC> shortestNPCs;
 List<NPC> tallestNPCs;
 List<NPC> otherNPCs;
 //lowest and highest value
 float maxHeight;
 float $$anonymous$$Height;
 
 void AssignNPCs(){
  
  maxHeight = float.NegativeInfinity;
  $$anonymous$$Height = float.PositiveInfinity;
  //get $$anonymous$$ and max values in array elements
  for(int i = 0; i < npcs.Length; i++){
   float npcHeight = npcs[i].height;
   if(npcHeight < $$anonymous$$Height) $$anonymous$$Height = npcHeight;
   if(npcHeight > maxHeight) maxHeight = npcHeight;
  }
 
  shortestNPCs = new List<NPC>();
  tallestNPCs = new List<NPC>();
  otherNPCs = new List<NPC>();
 
  //before you assing elements, decide on what to do if
  //maxheight and $$anonymous$$heigt are the same. 
  if(maxHeight == $$anonymous$$Height){
   //you could put all npcs in the otherNPCS list 
   //not sure if standard list has a constructor that takes an 
   //array as argument but oh wel. 
   otherNPCs = new List(npcs);
  }else {
   //assign elements
   for(int i = 0; i < npcs.Length; i++){
   //add tall npcs to tallest list, short to shortest, other to   //other.
    if(npcs[i].height == maxHeight) tallestNPCs.Add(npcs[i]);
    else if(npcs[i].height == $$anonymous$$Height) shortestNPCs.Add(npcs[i]);
    else otherNPCs.Add(npcs[i]);
   } 
  }
 }

Now you have lists of tallest, shortest, and in-between NPCs (and if they all have the same height they are all in inbetween).

avatar image siaran · Apr 25, 2015 at 01:41 PM 0
Share

So then what you do is you assign what lead to follow per list. So for example, all NPCs in the list of shortest ones you set it to leadLeft, for tallest to leadRight, and for other to leadCenter or something.

avatar image siaran · Apr 25, 2015 at 01:43 PM 0
Share

Alternatively, you could just order the array by element height, then assigne the first 1/3rd to left$$anonymous$$, second 1/3rd to center$$anonymous$$, and last 1/3rd to right$$anonymous$$.

I'm not completely sure what exactly you want actually, which is why this is all comments ins$$anonymous$$d of answers.

avatar image Fappp · Apr 25, 2015 at 02:20 PM 0
Share

Why not enum three different types of players? Tall medium and short. This way you can just perform if if's at the start of the process and pre-assign every NPC with his length.

Siaran is absolutely getting my support with comment additions because question is to vague imho.

I'm really bugged too as why the main player is a Non Playing Character... Please explain!

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

Does GetComponents() return the components in the order that they are displayed in the editor? 1 Answer

Awry Array 1 Answer

Need help with switch case in order/timed 0 Answers

Executing code with an order 1 Answer

Dynamic Turn Order and Display Based On Initiative Value (Javascript)? 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