Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Orggrim · May 22, 2013 at 09:07 AM · cameraarraytriggerontriggerenter

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KiraSensei · May 22, 2013 at 09:20 AM 0
Share

What are your triggers ? Are they UI buttons ?

avatar image Orggrim · May 22, 2013 at 09:21 AM 0
Share

cubes with trigger enabled

avatar image KiraSensei · May 22, 2013 at 09:23 AM 0
Share

So when your players hits one of the cube, you want to enable a different camera ?

avatar image Orggrim · May 22, 2013 at 09:23 AM 0
Share

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!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Orggrim · May 22, 2013 at 09:48 AM 0
Share

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

avatar image KiraSensei · May 22, 2013 at 09:57 AM 0
Share

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).

avatar image
0

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

   
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges