How to apply Vector to rotated object relatively

var cameraOffset = Vector3( 0, 5, 10 );
camera.transform.position = target.position + cameraOffset;

I want the camera to be placed behind the target 10 units away, and up by 5 units. But if the target rotates I want the offset to remain relative to being ‘behind’ the target. Currently the above script will offset the camera along the z axis independently of the target. How do I make it relative?

You can get the forward direction of the target via:


To get the position 10 units behind the target use:

target.transform.forward * -10

If you always want Y at 5 above the target you can then just set this directly:

Vector3 followOffset = target.transform.forward * -10f;
followOffset.y = 5f;
Vector3 followPosition = target.position + followOffset;

Otherwise you can get the target “up” vector, multiple by 5 and add this to followOffset.