Rotate camera while scene is running

How can i rotate the camera prefab to particular direction upon some action in the scene ?

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.

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

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

Hi,

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

So in your case you would need:

float fYaw = m_Prefab.transform.localrotation.eulerAngles.y;
m_Container.transform.Rotate(0, -fYaw, 0, Space.self);

P.S. use code tags around your code in this forum, it helps humans parse the code.

1 Like

Thanks @SiliconDroid . This solution works if rotation is around one axis.

Used following code for accomodating x axis rotation

float fYaw = m_Prefab.transform.localrotation.eulerAngles.y;
float fPitch = m_Prefab.transform.localrotation.eulerAngles.x;

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.

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:

m_ContainerYaw
    m_ContainerPitch
        m_Prefab

Careful of gimbal lock. :hushed:

1 Like

Thanks. It worked like a charm.

1 Like