- Home /
How can I have a 1st person and 3rd person camera movement option for the player?
I'm following this tutorial right now: https://unity3d.com/learn/tutorials/projects/roll-ball-tutorial/moving-camera?playlist=17141
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.
Do you want to have it toggle from first person to third person and vice versa??
Do you want to know how to switch camera or how to make a third person camera?
Answer by Spacejet13 · Jan 23, 2019 at 11:53 PM
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!!
Thanks so much! I've actually been hammering away at this and I've managed to implement the 3rd person view with a solution very similar to your suggestion!
if (thirdPerson == true && firstPerson == false)
{
transform.position = player.transform.position + offset;
}
But I'm having some trouble understanding how to do something similar for the 1st person camera view. I saw an example that used "transform.position = player.transform.position" and "transform.Translate(Vector3.forward)". Is there a way that I could do something like that too?
You're welcome! and yes, transform.position
is a good way of doing it through code!
(Also, it is good practice to accept answers that you feel like have answered your question, so others visiting with similar problems know that the accepted answer works!)
Your answer
![](https://koobas.hobune.stream/wayback/20220612190338im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Need idea for separate a surface of an 3D object 1 Answer
How do I use EditorGUIUtility.ShowObjectPicker()? C# 3 Answers
Multiple Cars not working 1 Answer