简体   繁体   中英

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;
        }
    }
}

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. 在此处输入图片说明

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

Please Look into this Camera.main Scripting Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM