user8434965
user8434965

Reputation:

Unity3D Camera Switching on trigger

I would really appreciate help on getting camera switching working in my game. The point is that the game starts, the player sees the ball(it's kind of the basic rolling a ball game), starts moving up and they need to go up again but in another direction, so i need the camera to be the way that they see what they're doing. I got the camera placements right, but I can't figure out how to script it the way that it would work correctly. I got it working once, but I messed something up and can't get it to work again.

This is the code so far, that I have put together from my searches:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraSwitch : MonoBehaviour
{
    public Camera front_camera;//assign your main camera here
    public Camera back_camera;//assign your top camera here

    void Start()
    {
        front_camera.enabled = true;
        back_camera.enabled = false;
    }
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.name == "Pickup")
        {
            front_camera.enabled = false;
            back_camera.enabled = true;
        }
        else if (other.gameObject.name == "Ground")
        {
            front_camera.enabled = true;
            back_camera.enabled = false;
        }
    }
}

Upvotes: 0

Views: 2243

Answers (2)

Monster Brain
Monster Brain

Reputation: 2119

I tried the code from Unity Scripting Manual and its working fine.

Please Look into this Camera.main Scripting Reference

Upvotes: 0

Muhammad Faizan Khan
Muhammad Faizan Khan

Reputation: 10541

It seems that your script is disabling (maybe its attache to camera) therefore, you are getting the problem. Make ensure that CameraSwitch is not disabling otherwise there is no problem with code.

Alternatively you can attach your camera switch script to the ball and placed two collider which cover front and back camera area respectively. something like this. enter image description here

Upvotes: 1

Related Questions