Search Unity

Question Place grandchild-objects on a circle perpendicular to the camera view

Discussion in 'Getting Started' started by ARunitystudi, Aug 9, 2021.

  1. ARunitystudi

    ARunitystudi

    Joined:
    Apr 25, 2021
    Posts:
    21
    I want to place my objects (grand-children of a parent) around their grandfather game-object on a circle that is perpendicular to the "forward vector" of the camera. The circle should be in the x/y-axis.
    My current solution is inspired by this question: https://answers.unity.com/questions/1068513/place-8-objects-around-a-target-gameobject.html
    Code (CSharp):
    1. for (int i = 0; i < num; i++) {
    2.     float angle = i * Mathf.PI * 2f / num;
    3.     Vector3 newPos = grandfather.transform.position + new Vector3(Mathf.Cos(angle) * num, Mathf.Sin(angle) * num, 4f);
    4.     grandChild.transform.localPosition = newPos;
    5. }
    Unfortunately, the grandChildren game-objects are not placed on a circle as intended. When using the global position for the grandchildren (= changing the last line of the code), the grandChildren are not rendered on screen. How can I fix that?

    Also, I am not sure how to adapt the code to be able to position the circle perpendicular to the "forward vector" of the camera and how to modify the radius of the circle?
     
    Last edited: Aug 9, 2021