Unity 5 c# - Cannot change between multiple cameras
I have a flat circular platform (modified cylinder) on which 4 cubes sit. The cubes are child objects of the platform, and the platform (and the cubes) rotate via a Constant Force torque. I have 5 cameras in the scene... one overlooking the entire platform, and the other 4 looking at each of the cubes close-up (one camera is a child of each cube, and so is rotating with its cube so its cube is always in close-up view).
I've been trying to get it so that when the overhead camera is active, by clicking on a cube its respective camera becomes the main camera so the view changes to see it close up. I have a "back" button in the scene which switches back to the camera overlooking the whole scene (this part works fine), but after 2 days I'm still not having any luck.. I have the script attached to each Cube object but the camera still changes between the overhead camera and only the first cube's camera. Code below. camera1 is the overhead camera, and camera2 is each cube's respective camera. Can anyone help?
using UnityEngine; using System.Collections;
public class ChangeCharCamHuman : MonoBehaviour { public Camera camera1; public Camera camera2;
void Start()
{
camera1.enabled = true;
camera2.enabled = false;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit = new RaycastHit();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (camera1.enabled == true)
{
camera1.enabled = false;
camera2.enabled = true;
}
else
{
camera1.enabled = true;
camera2.enabled = false;
}
}
}
}
}
Answer by Draedis · Apr 11, 2016 at 05:16 AM
Problem fixed! The platform had a collider active on it. After I deactivated the collider, everything worked fine with a couple more code tweaks. If anyone's interested in my solution, pls let me know :)