Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 AlastorSparda · Jul 27, 2011 at 08:03 PM · cameralookat

Look at multiple cameras

Hi there, i have a small problem right now, i have a scene with four cameras on it and i want and object to lo look at them when they are active, i have the following script for the cameras:


 var camera1 : Camera; 
 var camera2 : Camera;
 var camera3 : Camera;
 var camera4 : Camera;
 public var startCamera : int = 1;
 
 function Start () 
 { 
    camera1.enabled = true; 
    camera2.enabled = false;
    camera3.enabled = false; 
    camera4.enabled = false; 
    startCamera = 1;
 } 
 
 function Update () 
 { 
    if (Input.GetKeyDown ("c") && (startCamera == 1))
    { 
       startCamera = 2;
       camera1.enabled = false; 
       camera2.enabled = true; 
       camera3.enabled = false;
       camera4.enabled = false;
    } else if (Input.GetKeyDown ("c") && (startCamera == 2))
    { 
       startCamera = 3;
       camera1.enabled = false; 
       camera2.enabled = false;
       camera3.enabled = true;
       camera4.enabled = false;
    } else if (Input.GetKeyDown ("c") && (startCamera == 3))
    { 
       startCamera = 4;
       camera1.enabled = false; 
       camera2.enabled = false;
       camera3.enabled = false;
       camera4.enabled = true;
    } else if (Input.GetKeyDown ("c") && (startCamera == 4))
    { 
       startCamera = 1;
       camera1.enabled = true; 
       camera2.enabled = false;
       camera3.enabled = false;
       camera4.enabled = false;
    } 
    
 }



The function look at works great, but i can't find a way to switch the target of the look at acording to the camera active.

Thanks in advance

Comment
Add comment · Show 3
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 AlastorSparda · Jul 27, 2011 at 08:04 PM 0
Share

sorry for the mess with the code...

avatar image Chris D · Jul 27, 2011 at 08:28 PM 0
Share

Edit your question, highlight the code, and apply the changes. It'll get more people looking at your problem.

Remove the ` characters just to be sure.

avatar image AlastorSparda · Jul 27, 2011 at 08:39 PM 0
Share

sorry for the mess

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Jul 27, 2011 at 08:49 PM

You're talking about problems with lookat but don't post the lookat code. I guess it's in another script on that particular object. To what object is your camera script attached? The same object?

You just need your lookat script to access the current camera from the script above. Also you should really use an array for that task ;)

 var cameras : Camera[];
 var currentCameraIndex : int = 0;
 var currentCamera : Camera;
 
 function Start () 
 {
     ActivateCamera(0);
 }
 
 function ActivateCamera(index : int)
 {
     if (index >= cameras.Length)
     {
         index = 0;
     }
     for (var i = 0; i < cameras.Length; i++)
     {
         cameras[i].enabled = (i == index);
     }
     currentCameraIndex = index;
     currentCamera = cameras[currentCameraIndex];
 }
 
 function Update ()
 {
     if (Input.GetKeyDown ("c"))
     {
         ActivateCamera(currentCameraIndex + 1);
     }
 }

From your lookat script you have to access the cameraScript (GetComponent or assign it in the inspector) and read the currentCamera variable.

 var cameraScript : CameraScript;
 
 [...]
     transform.LookAt(cameraScript.currentCamera.transform);
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 AlastorSparda · Jul 27, 2011 at 09:40 PM 0
Share

that looks perfects, i just don't quiete understand how to link the camera script with the look at script

avatar image Bunny83 · Jul 27, 2011 at 10:24 PM 0
Share

You've read my questions above? How does you setup look like? What objects do you have in the scene and what script is on what object?

It doesn't make much sense to enumerate all possible ways of accessing other objects. Here are the most common ways, you should pick the one that fits your case:

http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

avatar image
0

Answer by Unamine · Jul 27, 2011 at 08:37 PM

Do not quite understand your question, but you want to look at an object q the active camera? If so, do the code-index, with an array;) Here is an example of a change in your code (I do not know if it works)

var cameras : Camera []; var index : int = 0;

function Update () { Camera.enabled = cameras[index]; }

if (Input.GetKeyDown ("c")) { index = index + 1; }

if (index> cameras.length) { index = 0; }

The index will serve to identify which camera is active, and when you press 'c' changes to the next camera set in the array of the inspector, if the index number is greater than the length of the array of cameras, back to the first :D

I hope I have helped, vote + 1 if I helped, and post any questions :D

...or use the Camera object to start variable you created, for example:

transform.LookAt (Camera [startCamera]);

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 AlastorSparda · Jul 27, 2011 at 08:44 PM 0
Share

Sorry if it wasn't clear, i have a mesh on my scene that i want to be always looking at the active camera, i have 4 cameras on my scene and i switch between them whith the code i posted before...

Thanks a lot

avatar image Bunny83 · Jul 27, 2011 at 08:58 PM 0
Share

Your array approach is of course better than an if-else chain ;) but:

  • The Camera class don't have a static member "enabled" of type Camera which could be assigned.

  • Your two if statements are "floating" outside of Update?!

  • You need to enable and disable all cameras the way you need it. If you switch from 0 to 1 you need to make sure that all cameras are disabled except "1".

  • I guess you meant transform.LookAt (cameras[startCamera-1]);

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Quaterion.Slerp + Lookat, camera flipping 0 Answers

Prevent camera from rolling while looking at object 1 Answer

How to implement lock on with a camera that is not a child of the player object? 1 Answer

Move camera relative to current position 0 Answers

How to rotate bone relative to camera using LookAt 3 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