- Home /
Array for multiple cameras?
Hi, I was looking for a way to put multiple cameras in an array, and when you enter a trigger, it would snap to a certain camera depending on the trigger
Now the way I thought of doing this is when you enter a trigger, you feed the array[i] value the appropriate number, but if there are alot of cameras in a scene, this could become time consuming, assuming you have to create a separate script for each trigger.
Is there an easier way to do this with OnTriggerEnter()? Or are there easier methods to do this
Not asking for code at all, but should you do, please try to explain it, tryin to learn here =D
Please let me know if you need any additional info!
Thanks again for your time.
So when your players hits one of the cube, you want to enable a different camera ?
thats the idea, I already have it set up to where I have to manually make each camera active or not in the script, and it takes forever!
Answer by KiraSensei · May 22, 2013 at 09:40 AM
So you need to declare an array of cameras in your player script. Each camera must be disabled but the one you want to start with.
Then in the method OnTriggerEnter (also in your player script), you need to collect the important information for you (it can be the name of the cube for example).
You could have something like that :
function OnTriggerEnter (other : Collider) {
switch (other.name)
{
case "Trigger1":
EnableCamera(1);
break;
case "Trigger2":
EnableCamera(2);
break;
case "Trigger3":
EnableCamera(3);
break;
}
}
Then you declare a method EnableCamera that will disable the current enabled camera (you can memorize the index in a variable to avoid disabling all cameras) and enable the good one in your array.
I just had an idea, if I were to make a prefab of a trigger area with a camera attached to it, could I do something like
OnTriggerEnter()
{
if(tag == "player")
{
GetChild.renderer.active
}
}
would this idea work at all? only problem I could see is how to deactivate all the other cameras
Ok so each camera is a child of a cube. Your code can work too, with GetChild.renderer.SetActive(true)
ins$$anonymous$$d of GetChild.renderer.active
But you still need to deactivate the other activated camera. So the cube needs to access the list of cameras by accessing the variable in the player script, or find them in the scene (this last solution costs a lot with the method "Find").
With my code, you only need to fill the array of cameras in the inspector with the correct game objects. Everything is controlled by the player (so you don't need to have a script to each cube).
Answer by kensct · May 22, 2013 at 10:05 AM
The way i would do this is to have a public gameobject that has a List that contains all your cameras and call it "_cameras", then assign all your camera to this list in the inspector giving them all appropriate names. then on each trigger have a public Camera and assign the appropriate camera and a public Gameobject which you assign to the cameras gameobject.
Then use this script on your gameobject that contains all the cameras:
enter code here
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Cameras : MonoBehaviour
{
public List _cameras = new List();
public void EnableCam(Camera targetcam)
{
foreach(Camera camera in _cameras)
{
if(camera != targetcam)
{
camera.enabled = false;
}
else
{
camera.enabled = true;
}
}
}
}
Then use this script on your triggers that contains all the cameras:
using UnityEngine;
using System.Collections;
public class TriggerScript : MonoBehaviour
{
public GameObject cameraHolder;
public Camera targetCam;
void OnTriggerEnter(Collider other)
{
if(other.tag == "player")
{
cameraHolder.GetComponent<Cameras>().EnableCam(targetCam);
}
}
}
Your answer
Follow this Question
Related Questions
Finding What Material Is On The Object Player Collided With. 1 Answer
Camera does not trigger OnTriggerEnter-event 1 Answer
multiple Objects Enter OnTriggerEnter 2 Answers
Can't figure out how to use multiple triggers in single scene 1 Answer
Moving Trigger/Platform and Moving Player calls OnTriggerEnter every frame 0 Answers