Im trying to make a simple escape room game, so I have 4 different rooms in one scene (basically 4 different sprites of a room with different objects on them), and I hve two buttons that change the camera position.
The script looks like this:
public class CameraController : MonoBehaviour
{
public new Camera camera;
public Transform[] cameraPositions = new Transform[4];
private int roomNumber;
private void Start()
{
roomNumber = 0;
camera.transform.position = cameraPositions[roomNumber].position;
}
public void MoveCameraRight()
{
camera.transform.position = cameraPositions[roomNumber++].position;
Debug.Log(roomNumber);
}
public void MoveCameraLeft()
{
camera.transform.position = cameraPositions[roomNumber--].position;
Debug.Log(roomNumber);
}
}
I made 4 empty game objects that act like anchor points for the camera, and put them and the main camera onto the script.
I put this script on an empty game object, and then put it on the buttons and gave each button a different method.
In essence, the script works. The thing is that the first click doesnt register, and when I reach the last room, I get an out of index range exception, and when I try to go back to the first room, the first clicks dont register again, and for some reason the number of all rooms goes down by one (for example, the first room, which is number 0, changes to -1).
Is there any way Ican make this less glitchy? I’ve been told its possible to do this using a state machine but I’m not sure how to do it. Any help would be appreciated ^^