- Home /
[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.
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)
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
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.
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
Follow this Question
Related Questions
How to rotate a direction (create a direction from another direction) 2 Answers
How to get object to face direction it's flying rather than looking at the target 2 Answers
Get Direction from 2 Vectors - and Apply to Transform 0 Answers
Need some help with Navmesh agents backing off from player / circling player please. 0 Answers
Vector3.SignedAngle wrong direction when crossing the 0 point 1 Answer