Your script seems to be on the right track. You should describe what your script is not doing when you post a script so that it is clearer what problem to solve.
What you describe is rather simple. Your code seems to be a bit confused about what eulerAngles are and you've left out some rather important details about your implementation.
In order to mirror the rotation about the right axis, you would apply this to your first camera
//this is the other camera.
var other : Transform;
function Update() {
if(other) other.eulerAngles.x = transform.eulerAngles.x;
}
eulerAngles.x is your rotation about the right (x) axis and will make your camera look up or down. All you need to mirror the rotation is to set the rotation of your mimic camera to be the same as your lead camera.
To mirror all rotation, you would simply need:
other.eulerAngles = transform.eulerAngles;
You've left out the part where you are translating one of your cameras. I don't really know how you mean to move your camera so it's challenging to assume a given movement system.
Wherever you move your camera, you would need to do something like:
var rotateScale : float = -15.0; //rotate left 15 degrees per unit moved right
var movementAmount : float = something; //Where you get your movement from
camera1.position.x += movementAmount;
camera2.eulerAngles.y += movementAmount * rotateScale;
Here's a script to attach to your main camera that will move based on input axes;
Master.js
var other : Transform; //the slave
var moveSpeedX : float = 2.0; //units to move per unit of input
var rotateSpeedX : float = 5.0; //degrees per unit of input
var rotateSpeedY : float = -15.0; //degrees per unit of input
function Update() {
//Get input
var v : float = Input.GetAxis("Vertical") * time.deltaTime * rotateSpeedX;
var h : float = Input.GetAxis("Horizontal") * time.deltaTime;
//Move this
transform.position.x += h * moveSpeedX;
transform.eulerAngles.x += v;
//Move the slave
if(other) {
other.eulerAngles.y += h * rotateSpeedY;
other.eulerAngles.x += v;
}
}
EDIT 10/04/10
From the slave perspective
Master.js
var moveSpeedX : float = 1.0; //units to move per unit of input
var h : float = 0.0; //The horizontal input
function Update () {
h = Input.GetAxis("Horizontal") * Time.deltaTime;
transform.position.x += h * moveSpeedX;
}
Slave.js
var master : Master;
var moveSpeedX : float = 1.0; //units to move per unit of input
function LateUpdate () {
if(master) {
var script : Master = master.GetComponent(Master);
if(script && script.h) transform.position.x += script.h * moveSpeedX;
}
}
Copying rotation is the same as copying position above, but replacing transform.position with transform.eulerAngles.
Considering things from the slave perspective is really spreading your code out. I assume this may be because you want the script to just work with a prefab and require little-to-no setup. You can achieve this from the master perspective by having the slaves add themselves to their master, changing the first scripting to something like:
Master.js
var slaves : Transform[] = new Array(); //the slaves
var moveSpeedX : float = 2.0; //units to move per unit of input
var rotateSpeedX : float = 5.0; //degrees per unit of input
var rotateSpeedY : float = -15.0; //degrees per unit of input
function Update() {
//Get input
var v : float = Input.GetAxis("Vertical") * time.deltaTime;
var h : float = Input.GetAxis("Horizontal") * time.deltaTime;
//Move this
transform.position.x += h * moveSpeedX;
transform.eulerAngles.x += v * rotateSpeedX;
//Move the slaves
for(var slave : Transform in slaves) {
//If you wanted to use slave-specific values
//var script : Slave = slave.GetComponent(Slave);
//if(script) {
// slave.eulerAngles.y += h * script.rotateSpeedY;
// slave.eulerAngles.x += v * script.rotateSpeedX;
//}
slave.eulerAngles.y += h * rotateSpeedY;
slave.eulerAngles.x += v * rotateSpeedX;
}
}
Slave.js
var master : Transform;
function Start() {
//add yourself as a slave
var script : Master = master.GetComponent(Master);
if(script) script.slaves.push(transform);
}
EDIT 10/07/10
If you cannot get your deltas directly or use input or some such (as in the case where using physics), you will have to calculate them manually.
Master.js
var slaves : Transform[] = new Array(); //the slaves
private var oldPosition : Vector3; //the position we had last frame
private var oldRotation : Quaternion; //the rotation we had last frame
function Start() {
oldPosition = transform.position;
oldRotation = transform.rotation;
}
function Update() {
//We may have been moved. Get deltas
var deltaPosition : Vector3 = transform.position - oldPosition;
var deltaRotation : Vector3 = Quaternion.FromToRotation(oldRotation,
transform.rotation).eulerAngles;
oldPosition = transform.position;
oldRotation = transform.rotation;
//Move the slaves
for(var slave : Transform in slaves) {
var script : Slave = slave.GetComponent(Slave);
if(script) {
slave.eulerAngles.y += deltaPosition.x * script.rotateSpeedY;
slave.eulerAngles.x += deltaRotation.x * script.rotateSpeedX;
}
}
}
Slave.js
var master : Transform;
var rotateSpeedX : float = 1.0; //degrees per unit of input
var rotateSpeedY : float = -5.0; //degrees per unit of input
function Start() {
//add yourself as a slave
var script : Master = master.GetComponent(Master);
if(script) script.slaves.push(transform);
}
Note that from the master's perspective, we only calculate their delta's once per frame, whereas from the slave perspective, every slave would have to re-calculate.