Advertisemen
Hello guys,
In this short tutorial, I will show you guys how switch between cameras in Unity 3D.
First of all, add another two cameras in your project and name them - camOne and camTwo and leave the mainCamera as it is. After that position and rotate them like this -
Position Rotation
x y z x y z
camOne 3 1 -4 0 -30 0
cameTwo 4 1 -4 0 -60 0
mainCamera 0 1 -4 0 0 0
Or rotate and position them how ever you like as long you can see what is happening when switch between cameras.
And don't forget to uncheck camOne and camTwo their AudioListener.
Position Rotation
x y z x y z
camOne 3 1 -4 0 -30 0
cameTwo 4 1 -4 0 -60 0
mainCamera 0 1 -4 0 0 0
Or rotate and position them how ever you like as long you can see what is happening when switch between cameras.
And don't forget to uncheck camOne and camTwo their AudioListener.
Next, add three buttons in the scene, GameObject > UI > Button and name them camOneBtn, camTwoBtn and mainCamBtn. To change button's display name go to Canvas > camOneBtn > Text. Do same with rest of the buttons. After that position them in the following order.
x y z
camOneBtn 0 -181 0
camTwoBtn 135 -181 0
mainCamBtn -131 -181 0
Now create one empty gameObject GameObject > Create Empty and name it gameManager, One C# script, name it whatever you want, I will name mine camEvents and drag script into empty gameObject we just created.
In your script type these first and then I will explain line by line.
using UnityEngine;
using System.Collections;
public class camEvents : MonoBehaviour {
public Camera [] cams; // 1
public void moveCameraOne(){ // 2
cams[0].enabled = false;
cams[1].enabled = true;
cams[2].enabled = false;
}
public void moveCameraTwo(){ // 3
cams[0].enabled = false;
cams[1].enabled = false;
cams[2].enabled = true;
}
public void moveCameraMain(){ // 4
cams[0].enabled = true;
cams[1].enabled = false;
cams[2].enabled = false;
}
}
If you can see or not, there are numbers next to the codes.
In number 1, Camera array named cams will hold three camera we have created in unity.
Line no. 2, 3, 4 are pretty straight forward, when function no 2 gets called it will turn on camera no. 1 in the array and disable camera no. 0 and 2 and same with rest of functions.
Ok let's go back to Unity, go to GameManger and drag cameras into array camera object.
And click on camOneBtn, in its inspector panel under Button tap you will see On Click(), drag your gameManager into this and select moveCameraOne function and do same with rest of buttons.
Now, hit play and click on the buttons to switch between cameras.
Advertisemen
Tidak ada komentar:
Posting Komentar