• 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 Thison1911 · Jan 28, 2021 at 01:23 PM · rotationvector3ainavmeshdirection

[Solved] Formation System - Rotate Vector3 Positions around a point?

Hi there,

I'm working on a simple formation system, and thus far I am very pleased with how it turned out. However, now I would like to rotate all the Vectors around a pivot point, any ideas?

Currently I'm setting all the destinations through Vectors in a list with offsets to the point:

 private List<Vector3> TwoLineFormation(Vector3 startPoint, float spacing, Vector3 direction)
         {
             List<Vector3> targetPosList = new List<Vector3>();
             int count = selector.getSelectedNPCs().Count;
             Debug.Log(count + " NPCs selected");
             for (int i = 0; i < 2; i++)
             {
                 for (int x = 0; x <= count / 2; x++)
                 {
                     targetPosList.Add(startPoint + new Vector3(spacing / 4, 0, (spacing * x) - (spacing / 2 * count / 2) - spacing / 2));
                     targetPosList.Add(startPoint + new Vector3(-spacing / 4, 0, (spacing * x + spacing / 2) -(spacing / 2 * count / 2) - spacing / 2));
                 }
             }
             return targetPosList;
         }
 

As you see, I would like to use the unused variable direction for that purpose, but I'm quite unsure on how to do that.

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
2
Best Answer

Answer by BastianUrbach · Jan 28, 2021 at 03:20 PM

To rotate a point around a pivot, subtract the pivot, rotate the result and then add the pivot:

point = rotation * (point - pivot) + pivot;
(rotation is a Quaternion, e.g. created with Quaternion.AngleAxis or Quaternion.Euler)
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 Thison1911 · Jan 28, 2021 at 11:06 PM 0
Share

Hey thanks, I'll try it out

avatar image Thison1911 · Jan 29, 2021 at 01:41 AM 0
Share

You're a genius, it works :D

I still use a vector so I can easier use it in my project. For anyone who needs this I'll post it here:

     public static List<Vector3> LineFormation(Vector3 startPoint, float spacing, Vector3 direction)
     {
         List<Vector3> targetPosList = new List<Vector3>();
         int count = Selector.getSelectedNPCs().Count;
         Debug.Log(count + " NPCs selected");

             for (int x = 0; x <= count / 2; x++)
             {
                 Quaternion rotation = Quaternion.LookRotation(direction, Vector3.up);
                 Vector3 pointFirstRow = startPoint + new Vector3(spacing / 4, 0, (spacing * x) - (spacing / 2 * count / 2) - spacing / 2);
                 Vector3 pointSecondRow = startPoint + new Vector3(-spacing / 4, 0, (spacing * x + spacing / 2) - (spacing / 2 * count / 2) - spacing / 2);
                 targetPosList.Add(rotation * (pointFirstRow - startPoint) + startPoint);
                 targetPosList.Add(rotation * (pointSecondRow - startPoint) + startPoint);
             }
         
         return targetPosList;
     }

Basically this already is a two-row formation as a list of vectors you can assign to navmeshagents, just in case anyone needs it

Here's a demo vid https://www.youtube.com/watch?v=ADBb5OoscyI

avatar image
1

Answer by itstimetomakelol · Jan 28, 2021 at 02:25 PM

Add the objects you want to pivot around to an empty gameobject with the position you want to pivot around.

For example: If you want some objects to pivot around (5, 5, 0) with a distance of 4 from the pivot point Step 1: Create an empty gameobject with the position (0, 0, 0) Step 2: Make the position of the gameobjects (4, 0, 0) (-4, 0, 0) (0, 4, 0) (0, -4, 0) (These are examples you can change it later) Step 3: Make the gameobjects children of the empty gameobject. Step 4: Move the empty gameobject to (5, 5, 0) For rotation: Rotate the empty gameobject

Not sure if this is what you want but it's something.

Code for the steps:

 private void PivotGameObjects(Vector3 pivotPoint, params GameObject[] gameObjects)
 {
     GameObject pivotObject = new GameObject("PivotObject");
     pivotObject.transform.position = pivotPoint;
     for (int i = 0; i < gameObjects.Length; i++)
     {
         gameObjects[i].transform.SetParent(pivotObject.transform);
     }
 }

Hope the code works didn't test it.

Comment
Add comment · Show 1 · 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 Thison1911 · Jan 29, 2021 at 10:16 PM 0
Share

Sorry, but that's not exactly what I was looking for. I do not use any gameObjects, only vectors to set them as destinations for my navmeshagents. But thank you very much for your effort :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Get rotation from a vector direction. 1 Answer

How to get offset postition after rotation ? (Spent hours figuring it out) 3 Answers

3D nested Turret Prefab Rotation 1 Answer

getting AI to look Left, then Right, then resume path 1 Answer

Rotating a direction Vector3 by Quaternion 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