Move camera certain distance away from object, but keep object centered (with specific camera rotation)

I have a character of which the camera should be positioned so that it:

  • – has a certain distance from the character object (e.g. 10 units)
  • – has a specific rotation (e.g. 45° on the x-axis)
  • – keeps the object centered on the screen

My attempt so far:

public GameObject camera;
public GameObject character;
private Vector3 distanceToChar = new Vector3(10f, 10f, 10f);

void Update() {
  camera.transform.position = character.transform.position + distanceToChar
}

However, then, the object isn’t centered on the screen. How can I do so?

Thanks in advance!

If you make the camera object a child of the character object, it will follow the character object around as it moves.

You can use this line to have the camera face the character:

camera.transform.LookAt(character.transform);

And when the camera is facing the character, you can use this line to move the camera towards or away-from the character:

camera.transform.Translate(Vector3.forward * distanceDelta);