Change Camera – Unity3D (C#)


Change Camera – Unity3D (C#)

Switching between multiple cameras is familiar to everybody from racing games. You can get a view from rear, front, interior etc. It’s easy to implement. If you use two or three cameras, you can assign a key to change camera and make them disabled and enabled. If you need more cameras, you need an array to hold them.

At the beginning, we will create an array for cameras. All must be disabled except one at start. We will do this in start function. Then we need an action to change camera on runtime. You may assign any key or mouse button.

Create three cameras and a cube to test it. In start function, we made all cameras disabled and then made the first one enabled. After that, in update function, we increase the camera index to get next camera. We check if the current camera is the last one in array. If it is, current camera is the first camera in array.

Download source code here. Do not attach this script to any camera! If you do, you cannot reach the script when that camera be disabled.

©Coffee Break Codes – Change Camera – Unity3D (C#)


Leave a comment

Your e-mail address will not be published. Required fields are marked *

5 thoughts on “Change Camera – Unity3D (C#)