- Home /
Set Children into a circle.
Hello, I was developing one of my systems i am currently working on and i stumbled uppon a problem. My goal is that when everything is spawned as a child, to put them into a circular shape. But what happens is that every child rotate around its own axis and just turns.
Why don't you apply this when instantiating you may ask? Well my system does allot of calculations and positioning before and that information is crucial for the system to work, that is why this piece of code is executed afterwards when all is done.
function Cilinderizer(){
//total amount of childs that will be there
var shaftCount:int=ShaftRowX*ShaftRowY*ShaftRowZ;
var i:int=0;
var children = GetComponentsInChildren (Transform);
for (var child : Transform in children) {
i++ ;
var angle = i * Mathf.PI * 2 /shaftCount;
var pos = Vector3 (Mathf.Cos(angle),Mathf.Sin(angle),0) * CylinderRadius;
child.localPosition=pos;
}
}
Did I make a mistake or is it something else? I am not using a for loop such as for(var i :int; etc,ect){} Because the loop already iterates trough all the children needed.
What are your values for shaftCount and CylinderRadius?
shaftCount are 3 int values in the inspector wich are added together (count1+count2+count3=shaftCount), CylinderRadius is also a value in the inspector, normally this should deter$$anonymous$$e how big the circle will become.
Count1=20,count2=20,count3=1. CylinderRadius=10
EDIT on last coment i said they add up, but the count values are multiplied on each other.
Are you getting 'angle' values that you expect. Have you put a Debug.Log(pos) at the end of the loop to see if you are generating good positions?
Answer by StormSabotage · Dec 10, 2013 at 10:38 PM
][1]
Is that what you want? Just write it on C#:
void Start () {
Cilinderizer();
}
private void Cilinderizer () {
Transform[] childs = transform.GetComponentsInChildren<Transform>();
float angle = 360 / (childs.Length-1);
int i = 0;
float radius = 3;
foreach(Transform child in childs){
i++;
float currentAngle = angle + (angle * i);
child.localPosition = new Vector3(radius,0,0);
child.rotation = new Quaternion();
child.RotateAround(transform.position,Vector3.forward,currentAngle);
}
}
This is indeed what i was trying to achive, but how come your approach does work? isn't it in essence the same thing?
Try it by yourself ) it's no matter what position and rotation childs was, they will become a strong circle )
I compared the two now and i see what the problem was indeed, I feel stupid that i did not thought about the rotate around function. I only calculated the angle ins$$anonymous$$d of keeping track of the current angle, ins$$anonymous$$d of creating a new value based of the new calculation.
You are a big life saver here, thank you very much.
@StormSabotage did you see my questions? I was encouraging $$anonymous$$arkD to debug this himself. We can't just feed people code all day.
@flaviusxvii I just know what it feels like to waste many hours without any progress, yes we can't feed people code, but it wasn't hard one, i just wanted to stop his headache and let go forward =\
Your answer
Follow this Question
Related Questions
How do I get the global position 2 Answers
Placing GameObject as Child in Hierarchy Changes Its Position 1 Answer
Parent transform not following child transform 0 Answers
Where to find original unity Parent/Child script? 2 Answers
Position as a child 1 Answer