Rotating the Camera around the player using the mouse

Hi,

I’m creating a space game, and I’m trying to use quaternions to allow the player to orbit the camera around the ship using the mouse while the “Freelook” key is held down, similar to holding down alt and looking around in Arma/Dayz. I have a “CameraDolly” which smoothly matches the camera’s rotation to that of the SpaceShip. The child of the Dolly is the “CameraAxis” which manages how far from the CameraDolly the player has rotated the camera. This is just so I can work using localRotation instead of rotation. Therefore, to look around, the player will be modifying the localRotation of the CameraAxis.

FreecamYTarget is the new TargetYvalue and the FreecamTarget is the new target quaternion. The localRotation of the cameraAxis will slerp from its current value to the target.

            FreeCamYTarget = _cameraaxis.transform.localRotation.y+_playercontrols.Ship.Look.ReadValue<Vector2>().x;
            FreecamTarget = new Quaternion(_cameraaxis.transform.localRotation.x, FreeCamYTarget, _cameraaxis.transform.localRotation.z, 1);
            _cameraaxis.transform.localRotation = Quaternion.Slerp(_cameraaxis.transform.localRotation, FreecamTarget, Time.deltaTime * FreeLookSensitivity);

The CameraAxis localRotation constantly twitches between its default position and some random yvalue when it should just be orbiting around the ship as per the mouse input. I’ve attached a video:

Pretty sure the issue is down to my poor understanding of Quaternions, but I’d appreciate your advice since I’ve been scratching my head over this for days. Thanks

Fixed using the following code

            Quaternion yOffset = Quaternion.AngleAxis(_playercontrols.Ship.Look.ReadValue<Vector2>().x, Vector3.up);
            FreecamTarget = _cameraaxis.transform.localRotation*yOffset;
            _cameraaxis.transform.localRotation = Quaternion.Slerp(_cameraaxis.transform.localRotation, FreecamTarget, Time.deltaTime*FreeLookSensitivity);

Also moving the code from FixedUpdate to Update made the camera transition a lot smoother. Not too sure why.

Generally do only physics in FixedUpdate().

ALSO… happy you got it working, but since camera stuff is pretty tricky… I hear all the Kool Kids are using Cinemachine from the Unity Package Manager.

There’s even a dedicated Camera / Cinemachine area: see left panel of this forum page.

If you insist on making your own camera controller, do not fiddle with camera rotation.

The simplest way to do it is to think in terms of two Vector3 points in space:

  1. where the camera is LOCATED
  2. what the camera is LOOKING at
private Vector3 WhereMyCameraIsLocated;
private Vector3 WhatMyCameraIsLookingAt;

void LateUpdate()
{
  cam.transform.position = WhereMyCameraIsLocated;
  cam.transform.LookAt( WhatMyCameraIsLookingAt);
}

Then you just need to update the above two points based on your GameObjects, no need to fiddle with rotations. As long as you move those positions smoothly, the camera will be nice and smooth as well, both positionally and rotationally.