- Home /
Switch between Camera's
I have 2 camera's:
Camera01
Camera02
I have also this JS:
var Camera01 : Camera;
var Camera02 : Camera;
var Player : Transform;
var Switcher : boolean;
var Timer : float = 10;
var ReduceTimer : boolean;
function Update (){
if (Input.GetMouseButtonDown (0)){
Switcher = true;
Timer = 10;
}
if (Switcher == true){
ReduceTimer = true;
Camera01.transform.position = Camera02.transform.position;
Camera01.transform.rotation = Camera02.transform.rotation;
Player.transform.rotation.y = Camera01.transform.rotation.y;
}
else{
Camera02.transform.position = Camera01.transform.position;
Camera02.transform.rotation = Camera01.transform.rotation;
}
if (ReduceTimer == true){
Timer -= 1 * Time.deltaTime;
}
if (Timer <= 0){
Switcher = false;
ReduceTimer = false;
}
}
Everything works, but the problem is, that the Camera's have the same rotation and position as when I changed the camera's.
By example,
Camera01 (position = 0, 10, 0)(rotation = 0, 90, 0)
Camera02 = disabled
Left Mouse button
Camera02 (position = 10, 90, 0)(rotation = 45, 0, 0)
Camera01 = disabled
What I want is, if I switch the camera's from Camera01 to Camera02, I want that Camera02 have the same rotation and position as Camera01 (I can rotate the camera) and if I switch back, Camera02 have the new position and rotation of Camera01. etc
Hope you guys understand my question, sorry for the difficult explanation.
Answer by androids · Feb 24, 2015 at 10:35 PM
After hours of scripting, I finally got a better solution. Everyone who have the same problem I had, here is a whole Javascript you can use for Your own project!
var target : Transform;
var distance = 6;
var distanceMin = 3;
var distanceMax = 6;
var yMinLimit = -16;
var yMaxLimit = 50;
var xSpeed = 13;
var ySpeed = 13;
var Switcher : boolean;
private var x = 0.0;
private var y = 0.0;
function Start ()
{
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
}
function Update ()
{
//This is a test key to see if everything is working
if (Input.GetKeyDown ("c"))
{
Switcher = !Switcher;
}
}
function LateUpdate ()
{
if (target)
{
if (Switcher == true)
{
x += Input.GetAxis ("Mouse X") * xSpeed * distance * 0.2;
y -= Input.GetAxis ("Mouse Y") * ySpeed * 0.2;
}
if (Input.GetMouseButton (0) && Switcher == false)
{
x += Input.GetAxis ("Mouse X") * xSpeed * distance * 0.2;
y -= Input.GetAxis ("Mouse Y") * ySpeed * 0.2;
}
y = ClampAngle (y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler (y, x, 0);
var position = rotation * Vector3 (1.0, 0.0, -distance) + target.position;
var hit : RaycastHit;
if (Physics.Linecast (target.position, transform.position, hit))
{
distance -= hit.distance;
}
if (Switcher == true)
{
position = rotation * Vector3 (1.0, 1.9, -distance) + target.position;
distance = 3;
yMinLimit = -30;
yMaxLimit = 30;
}
else
{
position = rotation * Vector3 (0.0, 1.9, -distance) + target.position;
distance = 6;
yMinLimit = -16;
yMaxLimit = 50;
}
transform.rotation = rotation;
transform.position = position;
}
}
static function ClampAngle (angle : float, min : float, max : float)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
Here you go!
Answer by siaran · Feb 24, 2015 at 12:11 AM
You need to keep track of which camera is active. If you only have 2 camera's you can do that with a boolean, if you have more you should make an enum or something.
then you'd make a method like
//This keeps track of which camera is active
bool cameraTracker = true;
function SwitchCamera(){
//Go from Camera1 to Camera2
if(cameraTracker){
Camera2.transform.position = Camera1.transform.position;
Camera2.transform.rotation = Camera1.transform.rotation;
Camera2.enabled = true;
Camera1.enabled = false;
cameraTracker = false;
}
//Go from Camera2 to Camera1
else{
Camera1.transform.position = Camera2.transform.position;
Camera1.transform.rotation = Camera2.transform.rotation;
Camera1.enabled = true;
Camera2.enabled = false;
cameraTracker = true;
}
}
I usually do C# and there are other problems with that code, but you get the idea... At least I think that is what you want?
Or do you mean that when you switch camera you want the other one to reset to it's initial position? In that case, store it's initial positions and set it back to those when you switch.
Edit: also change camera tracker bool on switch.
hmmm it's doesn't work or I just have no idea how to fix the js. By the way, it's all about 2 camera's. Please help
eh, I forgot to change the cameraTracker variable in that snippet when I change camera. Changed things around a bit so it makes more sense.
Nope, it's not working. This freaks me out. I'm trying to make an "OrbitCamera" arround the player and an "AimCamera" to fire. But still no succes.. have you any ideas?