How can I fit a camera so that sprite takes up entire view?

I have a Camera which points at a Sprite, both are on the same plane and sit about 20 units apart. The Camera outputs to a RenderTexture, which has the exact same size as the Sprite.

Currently, this results in the following (where the Camera background is Solid Red):
Imgur

I’m trying to figure out how to, programatically via code, change the Camera position (or FOV…?) such that the Sprite fully takes up the Camera view exactly. This should always be possible, because the RenderTexture and the Sprite have the exact same size.

Desired outcome:
Imgur

Looking for any suggestions or code examples that may help me achieve this. Currently I can do this manually by editing the Camera inspector values, but I’d like to get this done automatically!

well how I personally would do it is use multiply different cameras with different settings.


for example:
In a pokemon clone i was making when a player went from roaming mode to battle mode we just turned 1 camera Off and turned another On. Programmatically in the update when hits equaled 0. each step brought the counter down 1 increment to 0 then started battle / changed cams. One was attached to the player moved with it the other was pointed in some other direction and had a canvas hiding the scene showing the battle animations over top. if all you are trying to to is focus view on that image then make it have a canvas attached to its own camera and set the setting up to the render mode to screen space camera and scale with screen size on the canvas scaler. set the size of ur preview scene. Fill the canvas with ur image. disable camera GameObject. leave main enabled.

in code

GameObject mainCamera;
GameObject ImageCamera;

//Can be used in unity as a button to test.
public void SwapCams(bool enabled)
{
    mainCamera.SetActive(enabled):
    ImageCamera.SetActive(!enabled):
}