Transform global quaternion from local quaternion

I’m making a racing game camera.

I have the “Vehicle” wich moves arround and I have a camera following the vehicle. This camera is held by an empty gameObject wich I’ll name CameraHolder.

In the Hierarchy the Camera is son of the CameraHolder, wich is son of the Vehicle.

  • Vehicle
    • CameraHolder
      • Camera

The camera is always at the same distance from the CameraHolder and I rotate the cameraHolder using mouse.

angleX += Input.GetAxis("Mouse X")*sensitivity;
angleY += Input.GetAxis("Mouse Y")*sensitivity;

var wantedRotation : Quaternion = Quaternion.Euler(-angleY, angleX, 0);

transform.rotation = toGlobal(wantedRotation);

The problem is that I want angles to be relative to the Vehicle’s rotation so I need to transform the local Quaternion wantedRotation generated from angles, to global Quaternion using the Vehicle rotation quaternion in some way.

I need some way of toGlobal() function.

Probably, if it’s not possible I’ll need to work on Euler Angles or Directions.

sol 1

use local rotation directly

transform.localRotation = wantedRotation;

sol 2

use offset from another object’s rotation

transform.rotation = otherTransform.rotation * wantedRotation;

You can get the global rotation based on the parent objects global rotation and a local rotation like this;

Quaternion worldRotation= transform.parent.rotation * localRotation;