Search Unity

[SOLVED] Rotate camera on z-axis when x-axis rotation is 90 degrees

Discussion in 'Scripting' started by DivideByNil, Feb 13, 2016.

  1. DivideByNil

    DivideByNil

    Joined:
    Dec 4, 2015
    Posts:
    15
    I am making a little tilt marble labyrinth game as my first Unity project, and I am trying to get the camera to rotate simulating the act of tilting the game table. The camera is rotated at x = 90, y = 0, z = 0). This causes it to look directly at the center of the game board beneath.

    Rotating along the x axis works as expected with vertical input, but rotating along the z-axis simply spins the camera rather than rotate on its relative z axis -- and I understand why. Since the camera is already at 90 degrees, rotating on the z axis will produce the effect of spinning instead of rotating.

    My problem is that I haven't figured out how to solve this. I want to be able to rotate along the x and z axises given that x = 90 is center. I've been trying to hours to come up with the math that would solve this but have come up short. I must ask for your help!

    Here is an example of my code that produces the issue I described:

    Code (CSharp):
    1. Quaternion target = Quaternion.Euler (tiltAroundX+90, 0, tiltAroundZ);
    2. cameraController.transform.rotation = Quaternion.Lerp(transform.rotation, target, tiltForce);
    The cameraController is simply the component script of the camera object. TiltAroundX and tiltAroundZ are accelerometer input angles, Pretty basic, but here I struggle. tiltForce is just an argument to control tilt speed.

    Any help would be greatly appreciated, thank you!
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    This is a basic problem of euler angles. Taking it out of local rotation and instead using world location, or putting the camera into a prefab should work
     
  3. DivideByNil

    DivideByNil

    Joined:
    Dec 4, 2015
    Posts:
    15
    I'm not really sure how to do any of those things.

    I found no way to differentiate euler angles between local and world. The syntax doesn't seem to allow it. I don't know what you mean by put the camera in a prefab -- a prefab of what? To do what?

    Believe me, I tried everything I could think of before I asked for help... These suggestions are far too vague for me. Thanks anyway, though.
     
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Put the camera into a prefab then rotate that instead.
     
  5. DivideByNil

    DivideByNil

    Joined:
    Dec 4, 2015
    Posts:
    15
    After tooling around for a half-hour trying to work with a prefab I was able to accomplish nothing. I have no idea what to do.

    Can you walk me through this? I would appreciate it.
     
    Last edited: Feb 13, 2016
  6. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Create an empty gameObject, put the camera in it, rotate the gameObject, Done
     
  7. DivideByNil

    DivideByNil

    Joined:
    Dec 4, 2015
    Posts:
    15
    gorbit99, thank you for your help! Because of your clear suggestion I was able to solve the problem by placing the camera on a parent dummy object with the camera itself pre-rotated, just like you said! I can now rotate the camera (by rotating the parent object) as desired.

    Thanks for your patience and help as I learn to use Unity.
     
    Last edited: Feb 13, 2016