Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Child object rotation upside down when facing left

Discussion in 'Scripting' started by GNGification, Jun 13, 2016.

  1. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    I have an empty object called "Player", which has several child objects. When turning left or right I rotate the Player object but when I make the player look towards the mouse, I rotate several child objects (aka TopBody). Problem is, when facing left the rotation is upside down. It works perfectly when facing right tho.

    Code (csharp):
    1.  
    2. foreach(GameObject GO in TopBody)
    3. {
    4.        Vector3 diff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;              
    5.        diff.Normalize();
    6.  
    7.        float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
    8.  
    9.        //rot_z 0 = right, 90 = up, 180  = left
    10.        if (rot_z < 180 && rot_z > 90) //left-up //this is the part that doesn't work so well
    11.        {
    12.              if (!isFacingLeft)
    13.              {
    14.                     RotateLeft();
    15.              }
    16.              GO.transform.rotation = Quaternion.Euler(0f, 0f, rot_z);
    17.          }
    18.  
    19.          if(rot_z < 90 && rot_z > 0) //right-up //this part works perfectly
    20.          {
    21.                 if (isFacingLeft) RotateRight();
    22.                 GO.transform.rotation = Quaternion.Euler(0f, 0f, rot_z);
    23.           }
    24.  }
    25.  
    I've tried changing rotation values but so far nothing has worked.
    I also tried this:
    Code (csharp):
    1.  
    2. /*Vector3 temp = GO.transform.position;
    3.              temp.y += 180;
    4.              temp.z = rot_z;
    5.               GO.transform.eulerAngles = temp;*/
    6.  
    This part turns the object right way, but the aim is mirrored (when aiming high left, the rotation is low left and aiming low left rotation is high left, if that makes any sense."

    Any help is appreciated!
     
  2. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    You can calculate a quaternion much easier with just Quaternion.LookRotation. In your case you would use something like..
    Code (CSharp):
    1. Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    2. Quaternion targetRot = Quaternion.LookRotation(transform.forward, mousePos - transform.position);
    3. foreach (GameObject GO in TopBody)
    4. {
    5.     GO.transform.rotation = targetRot;
    6. }
     
    Last edited: Jun 13, 2016
    GNGification likes this.
  3. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    That worked well enough for me, had to do some adjustments but thank you.