Search Unity

How to set camera yaw (x) rotation to another gameobjects yaw (x) rotation.

Discussion in 'Scripting' started by Jordansavage01, Aug 16, 2019.

  1. Jordansavage01

    Jordansavage01

    Joined:
    Aug 16, 2019
    Posts:
    5
    I would like to set the camera's rotation to another gameobject's x-axis rotation, however, I would only like to do this once when the game starts.
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Something along the lines of
    Code (CSharp):
    1. void Start()
    2. {
    3.     Vector3 cameraRotation = myCameraObject.transform.eulerAngles;
    4.     cameraRotation.x = myTargetObject.transform.eulerAngles.x;
    5.     myCameraObject.transform.eulerAngles = cameraRotation;
    6. }
    You'll need to set up your variables to reference the appropriate objects, of course.
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You should be careful with using Euler angles like this ^ (specifically, reading in a Euler angle and then dividing it up by its components can cause problems). More info here. Basically, it might work, or you might get nonsense rotations because euler angles are terrible. Or, worst case, it might appear to work at first and then start failing later on, leaving you wondering why!

    If you could provide more information or context on what you're trying to accomplish specifically, I could provide a better strategy for getting to that goal.
     
  4. Jordansavage01

    Jordansavage01

    Joined:
    Aug 16, 2019
    Posts:
    5
    I would like to set the camera rotation the same way a gameobject is facing.

    At the moment this gameobject “A” faces a random way when the game starts so I would like the camera to face that way when it starts too.

    The game object is rotated using Quaternions (and maybe Euler angles) but that’s a different object. I would just liked the camera to face the direction where the game object is facing on startup
     
    Last edited: Aug 16, 2019
  5. Jordansavage01

    Jordansavage01

    Joined:
    Aug 16, 2019
    Posts:
    5
    I’ll give this a go and let you know! Thank you
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Well.... getting the "X" rotation won't solve that at all. Rotating around the X axis is up and down.

    Rather than futzing with Euler angles that will fail you any second, try something like:
    Code (csharp):
    1. void Start() {
    2. Vector3 targetForward = myTargetObject.transform.forward;
    3. targetForward.y = 0f;
    4. myCameraObject.transform.rotation = Quaternion.LookRotation(targetForward);
    5. }
     
  7. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    @StarManta, do you realize that your code zeroes the rotation on the two axes you are not matching? That doesn't really seem in the spirit of the question to me.
     
  8. Jordansavage01

    Jordansavage01

    Joined:
    Aug 16, 2019
    Posts:
    5
    None of the above worked...
     
  9. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    "It doesn't work" is not a problem description. Did you get an error? Did the camera rotate the wrong way? Did your computer burst into flames? Is your program acting exactly as it did before you added the code?

    What diagnostic steps have you already taken? Did you add Debug.Log statements to make sure your code is running at all?

    My silver-bullet guess is that you are successfully copying the object's rotation, but you are doing so before the object's rotation gets randomized, rather than after. But that's only a guess. There are dozens of things that could theoretically have gone wrong, most of which would be invisible from the other side of a forum if you don't provide any details.
     
  10. Jordansavage01

    Jordansavage01

    Joined:
    Aug 16, 2019
    Posts:
    5
    It didn't work - it didn't work as expected/this did not fix my problem. The camera does not face the same way as the object, my guess is it happens too soon too. No errors or anything.

    The cameras rotation does not seem to have changed.
     
    Last edited: Aug 17, 2019
  11. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    OK, so refactor your code in a way that things are guaranteed to execute in the order that you need them to execute.

    Some of the numerous ways to do that include:
    • Put both things in the same function (or make them both be called explicitly from the same function) in some explicit order
    • Do the first one in Awake and the second one in Start
    • Have the second one use polling in Update to repeatedly check whether the first one is done until it is
    • Have the second one wait for a fixed amount of time before running and hope that's good enough (in this case, it probably is)
    • Set up an event and a listener so that the second thing will be notified when the first thing is done
    • Modify your Script Execution Order
    Before you do any of those things, you could also consider confirming your hypothesis by adding Debug.Log statements to both things so you can check in your console what order they are currently executing in. In most cases, I would suggest verifying your diagnosis before attempting a fix. But in this case, the fix is something you should have done in the first place anyway, so it's probably a good idea to do it even if your immediate problem is something else.