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 Phenomenology · Sep 25, 2012 at 12:55 AM · 2dcoordinates

Arrange objects in a box formation?

Hello. I'm making a top-down game where "minions" can be spawned around and moved with the player. No matter what I try, though, I'm stuck when attempting to arrange said minions in a thickening box formation around the player. I've attached some code to show how I perform a "rank and file" formation in front of the player. I already have ways to change the formation, but I can't figure out how to calculate the minion's coordinates offset from the player for the box formation. Is there any way to do this?

EDIT: Here is a rough idea of how I imagined the minion order (imagine they're in a grid). I'm not sure if the third layer is a good pattern, so I'm open to modifications.

alt text

 public int myID;
 public float x = 0;
 public float z = 0;

 void AssignID (int minionID) { // called from another class
     myID = minionID; // starts at zero, and is incremented for each new minion
 }

 void FormUp () {
     if (myID % 2 == 0) { //go left
         x = 0.5f * (1 + myID % 10);//rank and file formation
         z = 1 * (1 + myID / 10);
     } else { //go right
         x = 0.5f * (myID % 10);//rank and file formation
         z = 1 * (1 + myID / 10);
     }
 }

 void Update {
 
     if (myID % 2 == 0) { // go left
         // offset the position of the minion from the player's position
         // "movement" is a class that deals with the player's movement and... 
         // ...position on the screen
         placement = new Vector3(movement.myX - x, 0, movement.myZ + z);
         iTween.MoveUpdate(gameObject, placement, 0.4f);
     } else { // go right
         placement = new Vector3(movement.myX + x, 0, movement.myZ + z);
         iTween.MoveUpdate(gameObject, placement, 0.4f);
     }
 }


arrangement.png (4.0 kB)
Comment
Add comment · Show 1
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 Phenomenology · Sep 25, 2012 at 05:08 PM 0
Share

Done! Thanks for the advice.

1 Reply

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

Answer by Fattie · Sep 25, 2012 at 05:40 PM

that's not particularly easy.

I'll tell you the simplest idea to get this done and move on...

To be clear you only need UP TO THREE LAYERS, correct?

Ok here's what to do. LOOK at any one number. Let's say 23.

You have to write down the "MOVE" for 23.

For 22, the "MOVE" is in fact -2,+3 . you get it?

For 7 the MOVE is +1, -1

ok?

Now do this.

 private var moves:Vector2[] =
         [
         Vector2( 2,-1 ),        // 0
         Vector2( 2,2 ),            // 1
         Vector2( 0,-1 )    ,        // 2
         .. etc .. do all of them up to 47 ...
         .. etc .. do all of them up to 47 ...
         .. etc .. do all of them up to 47 ...
         ];

You understand how to do the rest right?

Just make sure you have the local forward. Whatever your gap between them is (say 0.4 or whatever), just multiply by that. So it's like ..

for say number 15 ...

 goThisFarRight = moves[15].x * 0.40;
 goThisFarUp = moves[15].y * 0.40;

So those two are how far you move "15" from the "H" hero.

You could easily write a logical procedure that gives them to you, but it would be pointless because I guess you want the flexibility of knowing the layout your are using (ie, the one you drew)

I hope this does the trick ! Cheers! K.I.S.S. ... very important!


BTW

If you TRULY want a random position on layer N, it's just this

 function randomOnLayer(n:int ):Vector2
   {
   if (Random.Range(0,2)==0)
   return Vector2(Random.Range(-n,n+1),n*(Random.Range(1,3)*2-3));
   else
   return Vector2(n*(Random.Range(1,3)*2-3),Random.Range(-n,n+1));
   }

but really I recommend the "moves array" above.

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

10 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

Related Questions

How to get equal screen coordinates on different resolutions? 1 Answer

Fix a GUI's position relative to a 3D object 1 Answer

I want the camera to follow just the X coordinate of the player. 1 Answer

Find Mouse co-ordinates on click in a 2d environment 5 Answers

Implement display of object measures 0 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