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 /
  • Help Room /
avatar image
-1
Question by Ziron999 · Sep 16, 2015 at 08:42 AM · mathmathf

Trying to create unit formations

Right now i'm just trying to create a basic box formation...I've been doing it the hard way like this:

 void CreateBox(int i, int count)
 {
     if (i == 1)
     {
         boxX = 0;
         boxY = 0;
     }
     else if (i == 2)
     {
         boxX = 1;
         boxY = 0;
     }
     else if (i == 3)
     {
         boxX = 0;
         boxY = 1;
     }
     else if (i == 4)
     {
         boxX = 1;
         boxY = 1;
     }
     else if (i == 5)
     {
         boxX = 2;
         boxY = 0;
     }
     else if (i == 6)
     {
         boxX = 2;
         boxY = 1;
     }
     else if (i == 7)
     {
         boxX = 2;
         boxY = 2;
     }
     else if (i == 8)
     {
         boxX = 0;
         boxY = 2;
     }
     else if (i == 9)
     {
         boxX = 1;
         boxY = 2;
     }
     else if (i == 10)
     {
         boxX = -1;
         boxY = 0;
     }
     else if (i == 11)
     {
         boxX = -2;
         boxY = 0;
     }
     else if (i == 12)
     {
         boxX = -1;
         boxY = -1;
     }
     else if (i == 13)
     {
         boxX = -2;
         boxY = -1;
     }
     else if (i == 14)
     {
         boxX = -2;
         boxY = -2;
     }
     pos = new Vector2(boxX, boxY);
 }

What is a formula I can use to achieve the same thing? It is basically a box with points that need to be spaced out in each direction in 1 while stay centered at the point of origin 0,0;

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 sys12 · Sep 16, 2015 at 11:02 AM -1
Share

I'm not sure what you are trying to mean. $$anonymous$$aybe you can show some pictures.

If you know the exact value sets of x and y and the number of the sets is not so big, you can create a list of the set like boxPositionsListX and boxPositionsListY which contain all the values inside in order. Then fetch the values by boxPositionsListX[i].

avatar image Ziron999 · Sep 16, 2015 at 12:50 PM -1
Share

Hmm I'm no longer able to comment down there. Anyway it's not 14 that's just my stopping point as an example it should be possible to do a formula so you don't have to do this that's the whole point of this question "What is a formula I can use to achieve the same thing?" This is more of a mathematic question to do a box formation just like, well, like 99% of the real strategy games out there.. I think Raresh was on the right track but there is no way to pass the data to the destination and even if I did get it to work. That would be 3 total for's inside of one another so it's not really a solution.

avatar image Raresh Ziron999 · Sep 16, 2015 at 12:55 PM 0
Share

Check my latest comment below

avatar image Ziron999 Raresh · Sep 16, 2015 at 12:58 PM 0
Share

I did that's exactly what I just commented on..

3 Replies

· Add your reply
  • Sort: 
avatar image
-2
Best Answer

Answer by Ziron999 · Sep 16, 2015 at 01:00 PM

The solutions provided are far worse on performance and are not answers to my question. I'm going to close this and have just finished doing it the long handed way which seems to be better for performance anyways because i'm not using a single "for" statement this way. for's inside of for's is not a good idea you never wanna do that.

Comment
Add comment · Show 4 · 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 sys12 · Sep 16, 2015 at 01:17 PM -1
Share

https://danpearcymaths.wordpress.com/2012/09/30/infinity-program$$anonymous$$g-in-geogebra-and-failing-miserably/

Could probably what you want lol.

avatar image Suddoha · Sep 16, 2015 at 01:51 PM 3
Share

Nested loops can be very handy, there's no reason to claim "you never wanna do that".

If you prefer hardcoded logic in this case: that's what you should try to avoid, not only due to the fact that it will be unmaintainable and hard to extend.

avatar image Ziron999 Suddoha · Sep 17, 2015 at 07:01 PM 0
Share

you must not use the profiler much to see just how bad it is in any case no matter what you are doing. if you have to do for's inside of for's I highly suggest finding an alternative. at least until the quantum processor is finished haha

avatar image Ziron999 · Sep 17, 2015 at 06:54 PM 0
Share

sys12 you should of made that the answer you are the only one who was right on to what i'm looking for!!! The people who are giving you and me -'s obviously have no idea what math is...it ticks me off when people like them have over 100 rep and don't even know this type of stuff when it's crucial to program$$anonymous$$g a good game.

It also showed one other thing....it looks like only a hand full of people will even know the answer to this.

avatar image
2

Answer by Raresh · Sep 16, 2015 at 09:40 AM

     int distance=1; //distance between the units
     int i,j;
     int unitsY=4,unitsX=4; //how many units on X and Y
     
     for(i=0;i<unitsY;i++)
     {
        for(j=0;j<unitsX;j++)
        {
           boxX = j*distance+originX;
           boxY = i*distance+originY;
        }
 }

Comment
Add comment · Show 4 · 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 Ziron999 · Sep 16, 2015 at 10:50 AM 0
Share

this is very close but for some reason with:

 float distance = 1.5f; //distance between the units
         float y, j;
         float unitsY = count, unitsX = count; //how many units on X and Y
 
         originX = -count * 1.5f;
         originY = -count / 1.5f;
         for (y = 0; y < unitsY; y++)
         {
             for (j = 0; j < unitsX; j++)
             {
                 boxX = j * distance + originX;
                 boxY = y * distance + originY;
             }
         }

ok weird they are now going on top of each other and not going anywhere...

avatar image Raresh Ziron999 · Sep 16, 2015 at 11:02 AM 0
Share

I don't understand what you did with Origin X and Origin Y. They are supposed to represent a position set by you, not whatever you did there. Also is your game 2D or 3D. I think you might want to swap Y with Z

avatar image Ziron999 Raresh · Sep 16, 2015 at 11:17 AM 0
Share

it is 3d and I do already swap y with z...my original code works I think the problem with this is pos = new Vector2(boxX, boxY); is only called one time because it's not in the for's? so it only gets the ending result not each one?

Show more comments
avatar image
0

Answer by sys12 · Sep 16, 2015 at 11:39 AM

My approach would be:

     void CreateBox(int i)
         {
             var xList = new List<int>() { 0, 1, 0, 1, 2, 2, 2, 0, 1, -1, -2, -1, -2, -2 };
             var yList = new List<int>() { 0, 0, 1, 1, 0, 1, 2, 2, 2, 0, 0, -1, -1, -2 };
             pos = new Vector2(xList[i], yList[i]);
         }

Not sure what count is for.

Comment
Add comment · Show 7 · 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 Ziron999 · Sep 16, 2015 at 11:44 AM 0
Share

ok the reason for I and count is count can help with the origin by dividing the total amount (the count) by 2. I is the currently selected object that I need to assign a point to.

avatar image sys12 Ziron999 · Sep 16, 2015 at 11:51 AM 0
Share

I still don't understand. Do you have some more codes or some pictures?

avatar image Ziron999 sys12 · Sep 16, 2015 at 12:15 PM 0
Share

this is it

the red units are moving to the purple squares that's what I've done so far the long hard way and it works but...there must be a formula and more optimized way to do this.

Show more comments

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

29 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

Related Questions

Is Mathf.Infinity compatable with the burst compiled in Jobs? 0 Answers

How do I convert a number and a range to another directly proportional number and range? 4 Answers

Predicting the hit point on X axis, based on vector direction 1 Answer

What exactly does Mathf.InverseLerp() do? 4 Answers

How to know How many times the number was multiplied? 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