lakeviking
lakeviking

Reputation: 322

How to set the main camera to one of the other camera positions in Unity?

First of all, Unity is totally new for me. I have a room in Unity, and in each corner of the room there is a camera:

Camera1, Camera2, Camera3, Camera4

Now I want to place the main camera randomly in one of that other camera positions (so, with the same scale, position and rotation). So in fact one random camera should be set as the main camera. Each time I start the game, a new random corner should be chosen. Also when I open the game on my phone, after building the project.

Can anyone tell me how to do this?

Upvotes: 0

Views: 1889

Answers (2)

MaartenVlutters
MaartenVlutters

Reputation: 96

What i would personally do is not use 4 different cameras but use 1 camera that moves from position to position. using empty gameobjects and their Transform.position. You can make an array from wich you choose a random Object to set your camera position to

Upvotes: 2

Branden
Branden

Reputation: 347

First of all you should define your cameras in an array like this,

public Camera[] cameras;

Then populate the array within your inspector. Then you can toggle your currently selected main camera with some code that might look something like this:

int cameraIndex = Random.Range(0, cameras.Length); camera.Main = cameras[cameraIndex];

Upvotes: 1

Related Questions