Search Unity

Rotate camera while scene is running

Discussion in 'AR/VR (XR) Discussion' started by Rajawat, Jul 26, 2017.

  1. Rajawat

    Rajawat

    Joined:
    Oct 1, 2016
    Posts:
    25
    How can i rotate the camera prefab to particular direction upon some action in the scene ?
     
  2. SiliconDroid

    SiliconDroid

    Joined:
    Feb 20, 2017
    Posts:
    302
    Make the camera prefab be a child of an empty game object.
    Make the camera prefab be at position (0,0,0) and rotation(0,0,0) in local space of its new parent.
    Then rotate the gameobject, or call transform.lookAt on it.
    You could also translate it to teleport.
     
  3. Rajawat

    Rajawat

    Joined:
    Oct 1, 2016
    Posts:
    25
    Thanks SiliconDroid for the reply. I already tried this approach.
    At the start both camera prefab and camera container are positioned at 0,0,0 with a rotation 0,0,0
    Prefab is child of container. Prefab location is controlled by Gear.
    Aim
    If user looks at some particular direction then prefab's world rotation should be reset i.e 0,0,0

    Code:
    Vector3 eulerAngle = -1f * m_Prefab.transform.localrotation.eulerAngles;
    m_Container.transform.rotation = Quaternion.Euler (eulerAngle);

    Result
    Prefab is not looking exactly at zero after this computation.
    At the start
    AtStart.png After applying rotation. As you can see there is a slight tilt in z direction even though there is no rotation along z axis in inspector. P.S. Circle/Radial is converted to a dot/reticle
    AtEnd.png
     
  4. SiliconDroid

    SiliconDroid

    Joined:
    Feb 20, 2017
    Posts:
    302
    Hi,

    To change a users look direction you should only reset the yaw.

    So in your case you would need:

    Code (CSharp):
    1.  
    2. float fYaw = m_Prefab.transform.localrotation.eulerAngles.y;
    3. m_Container.transform.Rotate(0, -fYaw, 0, Space.self);
    4.  
    P.S. use code tags around your code in this forum, it helps humans parse the code.
     
    Last edited: Jul 27, 2017
    Rajawat likes this.
  5. Rajawat

    Rajawat

    Joined:
    Oct 1, 2016
    Posts:
    25
    Thanks @SiliconDroid . This solution works if rotation is around one axis.

    Used following code for accomodating x axis rotation
    Code (CSharp):
    1. float fYaw = m_Prefab.transform.localrotation.eulerAngles.y;
    2. float fPitch = m_Prefab.transform.localrotation.eulerAngles.x;
    3.  
    4. m_Container.transform.Rotate(-fPitch, -fYaw, 0, Space.self);
    Euler angle of container before Rotate call :{316.45, 139.97, 359.446}
    fPitch = 31.94, fYaw = 235.25
    Euler angle of container after Rotate call : {357.4, 275.31, 325.76}

    It should be {284.51, -95, 359.446}. I do not understand this computation.
     
  6. SiliconDroid

    SiliconDroid

    Joined:
    Feb 20, 2017
    Posts:
    302
    If you want 2 independent rot dimensions (that you can read and understand rotations as if they are 1D numbers) you can simplify things by building a 2D gimbal:

    Add extra m_Container so you have heirachy:

    Code (CSharp):
    1. m_ContainerYaw
    2.     m_ContainerPitch
    3.         m_Prefab
    Careful of gimbal lock. :eek:
     
    Last edited: Jul 28, 2017
    Rajawat likes this.
  7. Rajawat

    Rajawat

    Joined:
    Oct 1, 2016
    Posts:
    25
    Thanks. It worked like a charm.
     
    SiliconDroid likes this.