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