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 nolan_oc · Jan 29, 2021 at 10:55 PM · vector3transform.positionparent-childtransformdirection

Unit Formation Aligns to a Reset Forward Position Rather Than Parent Object's Rotation

Hello and thank you for clicking here. I'm in some trouble. My game requires units to move around in formation, and this is the function that creates the formation depending on how many units you have.

     public List<Transform> LineFormation(int numberInFormation)
     {
         foreach (Transform child in transform)  //destroy prevoius list of gameobjects so we do not add twice
         {
             Destroy(child.gameObject);
         }
         List<Transform> soldierPositions = new List<Transform>();
         for (int i = 0; i < numberInFormation; i++)
         {
             //create a list of children that can be accessed in the next loop
             GameObject position = new GameObject("position " + i.ToString());
             position.transform.parent = transform;
             soldierPositions.Add(position.transform);
         }
 
         int count = 0;
         float offsetX = offset;
         float offsetZ = offset;
 
         foreach (Transform soldier in soldierPositions)
         {
             Vector3 position = gameObject.transform.position;
             if (count == rowInt)    //if we have 7 soldiers in a row, start a new row
             {
                 count = 0;
                 offsetZ += offset;  //add a new row
             }
             //alternate between placements of positive x and negative x
             if (count % 2 != 0) //number is odd
             {
                 position.x -= offsetX;  //offset once to left
             }
             else
             {
                 if (count == 0) //first soldier is always set to middle
                 {
                     //do nothing so first soldier is naturally set to middle, reset the X offset
                     offsetX = offset;
                 }
                 else    //every even numbered soldier goes to right of middle
                 {
                     position.x += offsetX;
                     offsetX += offset;
                 }
             }
             position.z -= offsetZ;
             soldier.transform.position = position;
             count++;
         }
         return soldierPositions;
     }

So if the rotation on the parent object is not zero at the time of the formation creation( which will happen often in my game) you will always get a wonky looking formation.

here is an example where I rotated the Parent Object 90 degrees from 0 and sure enough the formation lined up 90 degrees off of the parent because they were aligning to zeroalt text

This is an example of how it looks when the parent transform's rotation is reset to 0 and how it should look no matter the rotation of the parent object at the call of my function. alt text

If you read this far thank you, and looking forward to your ideas.

badformation.png (175.2 kB)
goodformation.png (172.1 kB)
Comment
Add comment · Show 2
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 unity_ek98vnTRplGj8Q · Jan 29, 2021 at 11:25 PM 1
Share

I guess I'm not sure exactly what you are asking... do you just want the archers to always line up behind the parent soldier? So in the top picture you want the archers to be on the top half of the screen because the parent is facing down?

avatar image nolan_oc unity_ek98vnTRplGj8Q · Jan 30, 2021 at 06:07 PM 0
Share

sorry I see now that the screenshots were unclear. The parent object actually follows the player around, you can see it's rotation based on the axes but it isn't actually visible in game. Ideally I would like the row of archers to align with that parent object, so always facing in the direction of the blue axis.

1 Reply

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

Answer by jackmw94 · Jan 30, 2021 at 12:05 AM

I believe your problem is that you're using the soldiers' transform's "position" which is their global real-world space position, rather than their "localPosition" which is their position relative to their parent.

Change soldier.transform.position = position; to soldier.transform.localPosition = position; and you should be fine.

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 jackmw94 · Jan 30, 2021 at 12:06 AM 0
Share

This will also be true of rotation when you come to it

avatar image nolan_oc · Jan 30, 2021 at 08:31 PM 0
Share

Yes I just had to reset the position but you've totally fixed the offset rotation issue thank you!

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

142 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 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 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 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 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

Simple script to move object doesn't work 1 Answer

How to transform local space to world GYRO space on ios? 0 Answers

How to change transform.position.x to go backwards at specific instance? 2 Answers

Vector3 math problem? 1 Answer

Get closest Vector3 position from a GameObject and two Transforms (and the line inbetween them) 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