0
\$\begingroup\$

I have 3 cameras in a practice scene that I want to switch between. Currently, I have a meshless cube acting as a trigger with the following script

public class CameraSwitch : MonoBehaviour
{
    [SerializeField]
    private GameObject startCamera;
    [SerializeField]
    private GameObject camera2;

    // Start is called before the first frame update

    private void OnTriggerEnter(Collider other)
    {
        if(other.tag == "Player")
        {
            startCamera.SetActive(false);
            camera2.SetActive(true);
        }
    }

}

It's switching from the first camera to the second no problem, but it won't switch back if I move my player through the trigger again.

Is there a way I can store multiple different cameras inside one object and just disable every camera besides the one being used so I don't have to keep making triggers?

Any advice is appreciated.

\$\endgroup\$
1
\$\begingroup\$

Why not instead of having multiple cameras, have transforms (as waypoints) as reference for each Trigger Box? it would be cheaper on memory (since you are just having normal GameObjects, whereas in the case of cameras you make a little more memory per Camera Component). Example:

public class ExampleController : MonoBehaviour
{
    public Camera camera;

    private void OnTriggerEnter(Collider collider)
    {
        if(collider.tag == "Trigger(Or Whatever...)")
        {
            CameraTrigger trigger = collider.GetComponent<CameraTrigger>();
            if(trigger != null) trigger.SetCameraWaypoint(camera.transform);
        }
    }
}

With CameraTrigger being another script attached to the Trigger Boxes:

public class CameraTrigger : MonoBehaviour
{
    public Transform cameraWaypoint;

    public void SetCameraWaypoint(Transform camera)
    {
        camera.position = cameraWaypoint.position;
        camera.rotation = cameraWaypoint.rotation;
    }
}

Of course, there the problem would be to test the cameras on each waypoint, in which case you could make an Inspector class for that:

using UnityEditor;

[CustomEditor(typeof(CameraTrigger))]
public class CameraTriggerEditor : Editor 
{
    public override void OnInspectorGUI()
    {
        CameraTrigger myTarget = (CameraTrigger)target;

        if(GUILayout.Button("Test Camera")) myTarget.SetCameraWaypoint(Camera.main);
    }
}

It is a little bit (not so much) more complicated, but also more organized. Hope it gives an idea at least.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

You can create a camera manager script and put it in only one GameObject (let's call it GameObject A). It will contain all the references to the cameras stored in an Array. You will also have an index which will indicate which camera is currently on. You can then add a function that will take the index of the camera you want to enable as a parameter, disable the camera on the current index and enable the camera on the new index. Don't forget to update the new current index.

Then you can simply call this function through another GameObject (let's call it GameObject B) in OnTriggerEnter method.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

I think your triggerbox solution is efficient and in this instance it does not require a more complex way to solve it thats why i want to stick with it.

You can create a script which only contains a Camera reference.When it triggers it simply sets the referenced camera active.

This way you should get a clean solution.Only thing requires is proper desing in your scene using your trigger boxes.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.