- Home /
Code That Is Scene Dependent
I have this script that changes cameras based on the player position. I was wondering how I could make this so the functionality could change depending on the scene I'm in.
For example:
In scene 1, the camera changes if the player crosses Z=50, Y=20.
In scene 2, the camera changes only if the player crosses X=5.
I was thinking a separate script that would return a function that is either true or false, so I could just keep the script I have but just change the if statements. This is in C# mind you.
This is just an example snippet of code.
// Update is called once per frame
void Update () {
zPosition = playerCamera.transform.position.z;
if (zPosition > 0)
{
mainCamera.camera.active = false;
cam2.camera.active = true;
}
if(zPosition < 0)
{
cam2.camera.active = false;
mainCamera.camera.active = true;
}
}
Answer by Paulo-Henrique025 · Feb 16, 2013 at 06:13 AM
I think you want something like this:
if(Application.loadedLevel == 1)
{
//Camera 1 Stuff
}
if(Application.loadedLevel == 2)
{
//Camera 2 Stuff
}
Remember that the scenes must be added to the Build Settings
Its funny because I've used this method before. I just didn't think of it in this particular use for some reason. It does make for a very large piece of code, but Im not worried about that here. Thank you very much. Also note, this works if you use loadedLevelName as well if you don't want to be worried about the number level loaded!