- Home /
Having a main Camera rotate other cameras
Hello I have my main camera in a VR scene There are also window that display other scenes with render Textures (I have other cameras rendering to window textures)
I want the main camera movement to and fro to be reflected by the other cameras Rather than Parenting the cameras to the main camera (which almost works but gives too much movement & some weird feedback) I want to do this with script, so
My Main cameras rotations are passed to these secondary Cameras... I have no errors but nothig happens in my secondary cameras...
What am I doing wrong?
MAIN CAMERA: using UnityEngine; using System.Collections;
public class VRCamMain : MonoBehaviour {
//Storing the main Cameras values to pass to secondary cameras
//public float VRposX;
//public float VRposY;
//public float VRposZ;
public float VRrotX;
public float VRrotY;
public float VRrotZ;
void Update()
{
//Capture pos & rot
//VRposX = Camera.main.transform.position.x;
//VRposY = Camera.main.transform.position.y;
//VRposZ = Camera.main.transform.position.z;
VRrotX = Camera.main.transform.rotation.x;
VRrotY = Camera.main.transform.rotation.y;
VRrotZ = Camera.main.transform.rotation.z;
}
}
SECONDARY CAMERAS: using UnityEngine; using System.Collections;
public class VRCamSmall : MonoBehaviour {
// this calls the Main camera Script to get its values
VRCamMain _VRCamMain;
// storing myCamera positions locally
//private float posX;
//private float posY;
//private float posZ;
private float rotX;
private float rotY;
private float rotZ;
//naming ind camera
public GameObject myCamera;
//these are coming from other script
//public float VRposX;
//public float VRposY;
//public float VRposZ;
//public float VRrotX;
//public float VRrotY;
//public float VRrotZ;
void Start()
{
_VRCamMain = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<VRCamMain>();
//posX= myCamera.transform.position.x;
// posY=myCamera.transform.position.y;
//posZ= myCamera.transform.position.z;
rotX= myCamera.transform.rotation.x;
rotY= myCamera.transform.rotation.y;
rotZ= myCamera.transform.rotation.z;
}
void Update()
{
//posX = posX + _VRCamMain.VRposX;
//posY = posY + _VRCamMain.VRposY;
//posZ = posZ + _VRCamMain.VRposZ;
rotX = rotX + _VRCamMain.VRrotX;
rotY = rotY + _VRCamMain.VRrotY;
rotZ = rotZ + _VRCamMain.VRrotZ;
}
}
I Appreciate your help
~be
Answer by aditya · Nov 16, 2016 at 05:18 AM
There is nothing wrong with this code because there is nothing in this code despite of some variables with some values ... you've just fetched the values from VRCamMain class and did nothing with them, you need to apply these values to your secondary cameras
secondaryCamera.transform.localRotation = Quaternion.Euler(rotX, rotY, rotZ);
Accept if it did the trick
@aditya Thanks but still doesn't work. I put it in the Update Function, but it doesnt move at all...
It was my novice thinking that putting
rotX = rotX + _VRCam$$anonymous$$ain.VRrotX;
rotY = rotY + _VRCam$$anonymous$$ain.VRrotY;
rotZ = rotZ + _VRCam$$anonymous$$ain.VRrotZ;
in the update function would add the amount that tthe main Camera moved to the second Cameras..? Thanks!
Answer by Bentoon · Nov 17, 2016 at 03:09 AM
@aditya Almost! I'm Accepting this, although now the camera just Spins Wildly (although it seems to be coming a bit from how I move my head... ) Maybe I need a Fraction of the movement.. leading to stop when I stop moving the Main Cam (head tracking)?
MAIN CAM:
public class VRCamMain : MonoBehaviour {
// capture the Cam initial Pos
public float initPosX;
public float initPosY;
public float initPosZ;
public float initRotX;
public float initRotY;
public float initRotZ;
//Storing the main Cameras mov values to pass to secondary cameras
public float VRposX;
public float VRposY;
public float VRposZ;
public float VRrotX;
public float VRrotY;
public float VRrotZ;
void Start()
{
initPosX = Camera.main.transform.position.x;
initPosY = Camera.main.transform.position.y;
initPosY = Camera.main.transform.position.z;
initRotX = Camera.main.transform.rotation.x;
initRotY = Camera.main.transform.rotation.y;
initRotZ = Camera.main.transform.rotation.z;
}
void Update()
{
//Capture change in pos & rot minus initial
VRposX = Camera.main.transform.position.x - initPosX;
VRposY = Camera.main.transform.position.y - initPosY;
VRposZ = Camera.main.transform.position.z - initPosZ;
VRrotX = Camera.main.transform.rotation.x - initRotX;
VRrotY = Camera.main.transform.rotation.y - initRotY;
VRrotZ = Camera.main.transform.rotation.z - initRotZ;
}
}
SECONDARY CAMS:
public class VRCamSmall : MonoBehaviour {
// this calls the Main camera Script to get its values
VRCamMain _VRCamMain;
// storing myCamera positions locally
private float posX;
private float posY;
private float posZ;
private float rotX;
private float rotY;
private float rotZ;
//naming ind camera
public GameObject myCamera;
//these are coming from other script
public float VRposX;
public float VRposY;
public float VRposZ;
public float VRrotX;
public float VRrotY;
public float VRrotZ;
void Start()
{
_VRCamMain = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<VRCamMain>();
posX = myCamera.transform.position.x;
posY = myCamera.transform.position.y;
posZ = myCamera.transform.position.z;
rotX= myCamera.transform.rotation.x;
rotY= myCamera.transform.rotation.y;
rotZ= myCamera.transform.rotation.z;
}
void Update()
{
posX = posX - _VRCamMain.VRposX;
posY = posY - _VRCamMain.VRposY;
posZ = posZ - _VRCamMain.VRposZ;
rotX = rotX - _VRCamMain.VRrotX;
rotY = rotY - _VRCamMain.VRrotY;
rotZ = rotZ - _VRCamMain.VRrotZ;
myCamera.transform.localRotation = Quaternion.Euler(rotX, rotY, rotZ);
// myCamera.transform.localPosition = Quaternion.Euler(posX, posY, posZ);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Main Camera movement controlling others, rendering to texture 0 Answers
How to switch between two prefabs Cardboard cameras? 0 Answers
Unity XR get controller thumb stick axis 0 Answers