- Home /
Reverse object position order
Hi! I have a small problem maybe more mathematical than unity, but it's still the right place I think. I'm trying to reverse the object location order detect by a raycast (this maybe not be the right solution to use a raycast) in a click.
I explain myself with a drawing :
I used empty gameobject as a location for my objects, but I do not have the "code" in mind to operate a "conversely" ...
Because in drawing, there are only three objects, but I would like it to be a much larger number too. And with a possibility to click on any object also that suddenly, would operate a reversal with the objects only above him.
Thanks in advance :) !
Answer by Casiell · Jul 12, 2019 at 09:30 AM
So assuming you have the list of all the objects (either by raycast or other means):
Iterate through half the list
For each element "i" swap it's place with element "list.Count - i"
To swap place (assuming you want to swap their position) you need a Vector3 temporary variable
Something like this (not tested, a little bit pseudo-code'y):
List<GameObject> objects = getAllObjects
int count = objects.Count;
for (int i = 0; i < Mathf.FloorToInt(count / 2); i++)
{
Vector3 temp = objects[i].position;
objects[i].position = objects[count - i - 1].position;
objects[count - i - 1].position = temp;
}
After a little more thought, your solution was the good :)! Thank you so much.
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
GameObject facing hit.point doesn't always work 1 Answer
Flipping parent object in 2d with negative x scale causes rotation issues for children gameobjects 2 Answers
Make object only rotate in 90 degree increments 0 Answers
Distribute terrain in zones 3 Answers