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. Dismiss Notice

Question Inspector rotation axis is different in code?

Discussion in 'Scripting' started by babaliaris, Dec 18, 2020.

  1. babaliaris

    babaliaris

    Joined:
    Apr 21, 2013
    Posts:
    34
    This is the script which is attached to the door:

    Code (CSharp):
    1. public class DoorRotLimit : MonoBehaviour
    2. {
    3.     // Start is called before the first frame update
    4.     void Start()
    5.     {
    6.      
    7.     }
    8.  
    9.     // Update is called once per frame
    10.     void Update()
    11.     {
    12.         Debug.Log(transform.localRotation.eulerAngles.y);
    13.     }
    14. }
    When I rotate the door around the Z-axis, the code is telling me that the eulerAngles.y is changing not z.
    I'm not sure what is going on...

    Also, the door's local coordinate system seems to have Blender's with the Z-axis pointing up.
    I'm not sure if this is the problem, and Unity thinks that this is the Y axis.

    Check the following video where I rotate the door and check the inspector transform and the console log.

     
  2. babaliaris

    babaliaris

    Joined:
    Apr 21, 2013
    Posts:
    34
    I think I figured it out. I believe it was indeed because the objects keep blenders coordinate system, and I found out that I should always wrap imported models inside an empty game object and use that to apply transformations. Is this correct?
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Part of the problem you're running into is that Blender and Unity (infamously) use different coordinate systems. There's a little more to it than "Unity calls up 'Y' and Blender calls up 'Z'". Blender uses a so-called "right-handed" coordinate system. Unity uses a "left-handed" coordinate system. It's like the difference between these two pictures except that Blender also switches the Z and Y axes from the right-handed image:

    upload_2020-12-18_19-16-40.png

    One workaround is to wrap things inside an empty GameObject as you have done.

    Another workaround is to simply accept that the coordinate system will be a bit different upon importing. Go back into Blender and adjust your model until it is oriented properly when imported into Unity. Then you won't have to have your object rotated 90 degrees off-axis as you have in your video for it to look right. After doing that a few times you'll get a good idea of how models will look when imported into Unity from Blender.
     
    babaliaris likes this.