Search Unity

transform.rotation = Quaternion.Euler()

Discussion in 'Scripting' started by kittik, Aug 2, 2018.

  1. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    483
    I am missing something simple here, after trying to set a rotation to a predefined Vector3, nothing is happening. I am not sure if I should be using a different form of rotation, perhaps rid myself of the Quaternion.

    Code (CSharp):
    1.     void OnEnable()
    2.     {
    3.         topDownUI.SetActive(true);
    4.  
    5.         Screen.lockCursor = false;
    6.  
    7.         SetCameraAngle();
    8.     }
    9.  
    10.     void SetCameraAngle()
    11.     {
    12.         transform.rotation = Quaternion.Euler(storedRotation);
    13.     }
    The code in SetCameraAngle() does run via OnEnable, but nothing happens. I'm not sure why..
    Any feedback would be appriaicted.
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    6,457
    storedRotation
    is probably not what you expect.
     
  3. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    483
    That's what I first thought, however changing storedRotation to something like transform.rotation = Quaternion.Euler(60, 0, 0); still does not rotate the attached object. The method is called, logs are registered with in.
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    6,457
    The script is probably on the wrong object then.
     
    kittik likes this.
  5. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    2,078
    Make sure you're running transform.rotation on the correct object.
    Plus, make sure you don't want localEulerAngles instead of world .rotation.

    Also, make sure you're actually attached the script, and running that line of code. Just in case.
     
    kittik likes this.
  6. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    483


    The script is being run on the correct object as the above logs show (see red outlines).

    Code (CSharp):
    1.     void OnEnable()
    2.     {
    3.         topDownUI.SetActive(true);
    4.  
    5.         Screen.lockCursor = false;
    6.  
    7.         SetCameraAngle();
    8.     }
    9.  
    10.     void SetCameraAngle()
    11.     {
    12.         transform.rotation = Quaternion.Euler(60, 0, 0);
    13.         Debug.Log("SetCameraAngle()");
    14.         Debug.Log("I am attached to the object " + gameObject.name);
    15.     }
    There are two scripts, a 1st person angle, which switches to a Top Down camera (instead of having two cameras, maybe this is unorthodox!). I know two cameras would work, but this method should be working shouldn't it? I am using Unity 5.6, but I haven't come across this before. Maybe a different script is influencing the rotation also. Any further ideas? Help is appreciated.
     
  7. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    2,078
    If some other script is changing the rotation, it will be overwritten by different value. (which is, judging from screenshot, probably what's happening).

    Most likely you should move your "stored rotation" assignment to something like Awake/Start(), or something that runs before that other script.

    Or, even better solution. Merge the functionality of the scripts together.