- Home /
Make camera 1 transform equal to camera 2 transform
I have asked this before on here and multiple other sites, but no one has answered yet.
I made this Camera Switch script for the 2 cameras I use (They're both First Person) but when I'm looking straight up and switch to the other camera, the other camera is still looking straight forward or whatever direction I was looking at the last time I used camera2.
So how do I make the rotation of camera 1 to be equal to camera 2 and vice versa when I change my cameras?
This is my javascript:
var camera1 : Camera;
var camera2 : Camera;
var sound1: AudioClip;
var sound2: AudioClip;
var sound3: AudioClip;
function Start () {
camera1.gameObject.SetActive(true);
camera2.gameObject.SetActive(false);
SwitchCameras();
}
function Update () {
//right mouse button
if(Input.GetMouseButtonDown(1)) {
SwitchCameras();
}
}
function Playsound(){
audio.PlayOneShot(sound3);
}
function SwitchCameras(){
if(camera1.gameObject.active == false){
animation.Play("putUp");
yield WaitForSeconds(0.25);
audio.PlayOneShot(sound1);
camera1.gameObject.SetActive(true);
camera2.gameObject.SetActive(false);
}
else {
camera1.gameObject.SetActive(false);
camera2.gameObject.SetActive(true);
audio.PlayOneShot(sound2);
}
}
Can you clarify, do you want 2 cameras always at the same point/rotation and switch which one is rendered?
Answer by Chris_Dlala · May 26, 2014 at 04:21 PM
Hi, you can get any Monobehaviour's transform to get access to the GameObject's position, rotation and scale.
camera1.transform.position = camera2.transform.position; // Set camera1's position to that of camera2
camera1.transform.rotation = camera2.transform.rotation; // Set camera1's rotation to that of camera2
I'm not much of a JavaScript coder but here is an untested function to move a camera to the same position and rotation as another:
function MoveCamera( camera : Camera, sourceCam : Camera)
{
camera.transform.position = sourceCam.transform.position;
camera.transform.rotation = sourceCam.transform.rotation;
}
I hope that helps =)
I don't think it works or I implemented it wrong. Either way, I didn't get it to work. :(