- Home /
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
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.
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);
that looks perfects, i just don't quiete understand how to link the camera script with the look at script
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
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]);
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
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
Follow this Question
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