# How to change between camera angles in Unity

I need to change camera angles in Unity by hitting a key.

For example,

• I want to look down on the battle, so I hit 1.

• Then I want to see it from the side, so I hit 2, etc.

How do I write a script to do this change of perspective?

I have 3 cameras set up, each of them outputting to a different display. I want to switch between those cameras/displays to get a different viewing angle for the players. As an example, in the Unity editor, in the upper left corner, you have a tab that says Display 1. Clicking that will show you other displays.

Also, when setting up a camera, you have to connect it to a display. (I know you already know this, I just wanted to make sure you guys knew what I was talking about)

• You can do that with the unity addon, "cinemachine" I believe it is now built in with Unity, but if you can't find it, it's free on the asset store. Aug 25 '19 at 1:05

Displays on cameras are only used to target different monitors on a PC. If that's what you're going for, you can modify those values. If not, always set it to the first display.

Assuming you're using one monitor, All you need to do is make sure that you have those 3 cameras in the place you want them in the scene, but only have one active at a time.

So an example function could look like this:

void SetActiveCamera(int _whichCamera)
{
for(int i = 0; i < cameras.Length; i++)
{
if(i == _whichCamera)
cameras[i].SetActive(true);
else
cameras[i].SetActive(false);
}
}


cameras is an array of game objects - your camera game objects. You can assign this in the Inspector.

This function will turn on the camera you choose, and turn off all the others.

If you want a multi-monitor set up, all you need to do is have the proper Display configured for the camera and have them all enabled. But I don't think that's what you're going for.

in unity we have scenes. you have to implement you game in different scenes and change scenes using code below: using UnityEngine; using UnityEngine.SceneManagement;

public class ExampleClass : MonoBehaviour
{
void Start()
{
// Only specifying the sceneName or sceneBuildIndex will load the Scene with the Single mode
}
}


another approach to puth some gameObjects in a scene.then make camera show those scenes. this is my example:

list objList;

Update()
{

if(Input>GetKeyDown(KeyCode.Q))
{
Camera.main.transform.position= new Vector3(objList[0].transform.position.x,
objList[0].transform.position.y,
Camera.main.transform.position.z);
}

}

• It sounds like this could be done with one scene, just moving the camera, or reconfiguring objects in a single scene. Aug 24 '19 at 11:41
• @DMGregory there is lots of possiblities but this is the simplest approach for a beginner Aug 24 '19 at 11:45
• I strongly disagree with that statement. At minimum, now we have to set up two scenes instead of just one And persisting game state across scenes is an issue that trips up many Unity beginners. So I would say this proposal is very much not simpler than the alternatives. Aug 24 '19 at 11:48
• @DMGregory ill edit the answer Aug 24 '19 at 11:53
• I edited the question to include a few more details, I will add more if asked. @DMGregory Aug 24 '19 at 12:53