I am making a split screen co-op game so my scene has two camera in it, one for player one the other for player two. I want to move the objects relative to their respective cameras. The code I am using is below:
public float speed = 6.5f;
// Update is called once per frame
void FixedUpdate()
{
if (Input.GetAxis("RightStickVertical") < 0f)
{
rigidbody.AddTorque(Camera.main.transform.right * speed);
}
else if (Input.GetAxis("RightStickVertical") > 0f)
{
rigidbody.AddTorque(Camera.main.transform.right * -speed);
}
else if (Input.GetAxis("RightStickHorizontal") > 0f)
{
rigidbody.AddTorque(Camera.main.transform.forward * -speed);
}
else if (Input.GetAxis("RightStickHorizontal") < 0f)
{
rigidbody.AddTorque(Camera.main.transform.forward * speed);
}
}
I have used the Camera.main.transform.right for this, however since I don’t have a main camera, because I have two, I get a null reference as expected. Therefore my question is how do I set it for a specific camera in the scene?
The camera names are ballCamera and boxCamera.
I am using a xbox 360 controller.