This is for a third person camera game. I am trying to make a simple trigger volume to switch from the main camera that follows the player to a designated camera when the player enters it. When the player exits it, I want the camera to switch back to the main camera. I have made a component that does exactly that:
using UnityEngine;
using System.Collections;
public class OnCollisionChangeMainCamera : MonoBehaviour {
/* Public members */
public GameObject targetCamera;
public GameObject playerCamera;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider c)
{
if(c.gameObject.name == "Player")
{
if (!targetCamera)
return;
playerCamera.GetComponent<Camera>().enabled = false;
targetCamera.GetComponent<Camera>().enabled = true;
}
}
void OnTriggerExit(Collider c)
{
if (c.gameObject.name == "Player")
{
if (!targetCamera)
return;
playerCamera.GetComponent<Camera>().enabled = true;
targetCamera.GetComponent<Camera>().enabled = false;
}
}
}
Except for this weird error where the camera does not actually switch until the player has collided with it twice. So, if I enter/exit once, it does not work - but if I enter/exit after that, it works perfectly.
Could someone help me figure out why it’s doing this? Thanks.
This component is on a cube. The cube has the Is Trigger attribute set to true on its Box Collider component. There is no set Physics material on it. I have the Target Camera set to the camera I want it to switch to, and Player Camera set to the camera that follows the player.