How do you rotate an object relative to another with an offset ?

I wrote it with the help of a translator.

When I assign my spaceship rotation to the camera, everything is fine. But as soon as I add an offset to rotate the camera, with complex spaceship rotations, the camera goes crazy. I don’t understand Quaternions well enough to do anything with them, I only have a superficial understanding of them. How can I make the camera always have the same rotation as the ship with a given offset?

video : - YouTube

using UnityEngine;

public class FollowCamera : MonoBehaviour
{
    public GameObject target;
    public Vector3 offset;
    public Vector3 offsetRotate;

    public Vector3 rotationCamera;
    public Vector3 rotationShip;


    private void LateUpdate()
    {
        Vector3 newRotation = target.transform.localRotation.eulerAngles + offsetRotate;

        transform.rotation = Quaternion.Euler(newRotation);
        transform.position = target.transform.position + (target.transform.rotation * offset);

        // данные о повороте камеры и корабля
        rotationCamera = transform.rotation.eulerAngles;
        rotationShip = target.transform.rotation.eulerAngles;
    }

}

It sounds like you are trying to apply an offset rotation to the camera while maintaining its relative rotation to the ship. One way to achieve this would be to use Quaternion multiplication to combine the ship’s rotation and the offset rotation. Instead of using Quaternion.Euler to set the camera’s rotation, try using Quaternion.Euler on the offset rotation, and then multiply it with the ship’s rotation Quaternion. Then set the camera’s rotation to the result of that multiplication.
You should also ensure that your offset vector is also rotated by the ship’s rotation before applying it to the camera’s position.

transform.rotation = target.transform.rotation * Quaternion.Euler(offsetRotate);
transform.position = target.transform.position + (target.transform.rotation * offset);

Also, you can use Quaternion.LookRotation() to get the rotation needed to align the forward vector of the camera with the ship forward vector, and then you can add an extra rotation to it.

transform.rotation = Quaternion.LookRotation(target.transform.forward) * Quaternion.Euler(offsetRotate);

ACCEPT IT AS AN ASNWER IF IT WORKS!