- Home /
Switching Camera on Collision won't work
I have a script for which it is supposed to change the camera to get a better view of the cool explosion and display my game over screen. But it will not change and I am not certain why, there are no errors that show up and I have made sure to select the right game objects for my public variables.
Here is my code :
public Camera mainCamera;
public Camera explosionCamera;
void Start()
{
mainCamera.enabled = true;
explosionCamera.enabled = false;
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "Car Obstacle")
{
mainCamera.enabled = false;
explosionCamera.enabled = true;
}
}
Are you sure the OnTriggerEnter is being called? What components are you using with what settings?
Never$$anonymous$$d, I figured out my problem, I had just implemented the above script into another collision script which created an explosion, and ins$$anonymous$$d just changed the mainCamera's transform position as a replacement for the other camera. I also noticed that I was supposed to be using OnCollisionEnter ins$$anonymous$$d of OnTriggerEnter. Here is my other script that I had included this one in.
public GameObject explosion; AudioSource audio; public Camera mainCamera;
void Start()
{
audio = GetComponent<AudioSource>();
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Obstacle"))
{
Instantiate(explosion, transform.position, transform.rotation);
audio.Play();
mainCamera.transform.position = new Vector3(284, 85, -45);
mainCamera.transform.rotation = Quaternion.Euler(0, -90, 0);
}
}