Search Unity

Question Need help using rotation of another GameObject

Discussion in 'Editor & General Support' started by KLWN, May 4, 2020.

  1. KLWN

    KLWN

    Joined:
    Apr 11, 2019
    Posts:
    35
    Hello everyone,

    I have a small problem when using the rotation of GameObject A and use it for other GO's. Basically I have this cube and 3 cylinders (fbx model), one cylinder for each axis, in the code I have added the rotation values for each cylinder so they dont get overlaid.

    Now when I rotate the cube, the indicators should follow. I tried using the cylinders as childs but thats no option for me as to the scaling issue of child elements which I want to avoid.



    When I rotate the object the X and Y-Axis cylinder works fine except the one for the Z-Axis. Once I start rotating the object its all over the place, I also made a short video for demonstration...





    Code (CSharp):
    1.  
    2.     public GameObject CylinderXAxis;
    3.     public GameObject CylinderYAxis;
    4.     public GameObject CylinderZAxis;
    5.  
    6.     void Update()
    7.     {
    8.         var eulerAngles = this.transform.eulerAngles;
    9.      
    10.         Vector3 newScaleX = new Vector3(eulerAngles.x, eulerAngles.y, eulerAngles.z + 90);
    11.         CylinderXAxis.transform.eulerAngles = newScaleX;
    12.  
    13.         Vector3 newScaleY = new Vector3(eulerAngles.x, eulerAngles.y, eulerAngles.z);
    14.         CylinderYAxis.transform.eulerAngles = newScaleY;
    15.  
    16.         Vector3 newScaleZ = new Vector3(eulerAngles.z, eulerAngles.y - 90, eulerAngles.z + 90);
    17.         CylinderZAxis.transform.eulerAngles = newScaleZ;
    18.     }


    Any help is apreciated.

    best regards,
    Alex
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Make all of the objects children of an empty parent, then rotate the parent instead of the cube. Then you can scale any one of the child objects to your heart's content without touching the cylinders. Best of all, you don't need any scripts (other than for rotating the parent).

    upload_2020-5-4_14-3-20.png
     

    Attached Files:

    KLWN likes this.
  3. KLWN

    KLWN

    Joined:
    Apr 11, 2019
    Posts:
    35
    easy solution, I was totally overthinking it :) thanks, works like a charm