- Home /
Position gameObjects in circle around another gameObject
Hello, I am currently working on a game (in VR for Oculus Quest) where I have different platforms on which the player can stand. To these platforms belong a number of spheres, that should be placed in a circle around the center of each platform (a little bit above so that they are at a good spot to be grabbed by the player. My problem is that whatever I do, the spheres are always placed around the center of the world instead.
Currently the code where i'm positioning the spheres looks like this:
float angle = counter * Mathf.PI * 2f / spheres.Count;
Vector3 newPos = new Vector3(Mathf.Cos(angle) * 0.5f, 0.5f, Mathf.Sin(angle) * 0.5f);
sphere.GetComponent<Transform>().SetParent(transform);
sphere.GetComponent<Transform>().localPosition = newPos;
The script is placed on each platform and the code is within a foreach loop where I go through all spheres that have determined to belong to the platform.
I would really appreciate some help, so if you need any other information to help me with the problem, feel free to ask. I'm quite a beginner, but I'll try my best to provide the information. :)
Answer by superjustin5000 · Nov 02, 2020 at 02:46 PM
Make sure it's not a strange case where the pivot point of your platform is at world 0,0 . Or perhaps the actual physical platform object is a child of a parent game object, and you have the script on the parent?
These are off the top of my head outlier cases where I've experienced positioning issues.
Thanks a lot! The thing with the pivot point made me curious, and yes, the pivot point was centered in the world middle. right now i worked around it by using the renderer bounds and it works. :)
Nice! Glad you were able to work it out.