How can I have a 1st person and 3rd person camera movement option for the player?

I’m following this tutorial right now: Camera and Play Area - Unity Learn

But I was wondering how I’d make two buttons on the screen that would allow players to toggle on and off 1st and 3rd person camera views. So far, I’ve managed to display 2 buttons that are labeled with the camera view:

void OnGUI()
    {
        // 3rd person camera view
        if (GUI.Button(new Rect(20, 50, 140, 40), "3rd Person Camera"))
        {
        }

        // 1st person camera view
        if (GUI.Button(new Rect(20, 110, 140, 40), "1st Person Camera"))
        {
        }
    }

I noticed that in the above mentioned tutorial, he makes the main camera into a child of the player object, and I’m assuming that he is making this the only camera view for the whole game. But I’m not sure how to translate that (and a 1st person camera view) into button presses.

I am assuming you want to be able to toggle from first person to third person and vice versa

.

So to do this you will have to have 2 cameras in scene, only one active at a time. one camera will be positioned as a first person camera, and one third (you will have to drag and set the cameras in the scene view)

IMPORTANT: using my technique, you need to have both cameras child of player object!

So now that you have the cameras set up, in your script, you need to reference them.

public class CamViewChanger : MonoBehaviour
{
    public Camera firstPersonCam;
    public Camera thirdPersonCam;
}

Make sure that this script is on your player object (or any object if you want, but player is recommended by me).

Assign the values in the inspector - drag the first person cam and third person cam (children of player object) into respective slots in the script.

Then Make 2 public methods in the script

 public class CamViewChanger : MonoBehaviour
 {
     public Camera firstPersonCam;
     public Camera thirdPersonCam;

    public void SetToFirstPerson()
    {
        thirdPersonCam.enabled = false;
        firstPersonCam.enabled = true;
    }

    public void SetToThirdPerson()
    {
        firstPersonCam.enabled = false;
        firstPersonCam.enabled = true;
    }
 }

So now when you call SetToFirstPerson(), the third person camera will be disabled, and the first person camera enabled, and vice versa with SetToThirdPerson().

To call these functions from your button onClick, use This Link. It will take you to an answered question related to assigining onClick events to buttons.

Hope This Helped!!