- Home /
Changeing Camera Position
Hello, again. I've been trying to get a camera to jump to certain positions when the player hits the arrows keys. Basically if the player were to continuously hit the right arrow it would cycle through all the different camera positions. For some reason though when the player hits the right arrow nothing happens. I'm assuming that the code is being executed, but it is all happening at once so it cycles through all the positions and ends up back at the front position. Not really sure how to get it to stop on each position and wait for player input. My Code below.
var CameraFpos = true;
var CameraBpos = false;
var CameraRpos = false;
var CameraLpos = false;
function FixedUpdate() {
//Camera Right Spin from CameraFpos
if (CameraFpos && Input.GetButton("Right"))
{
Camera.main.transform.position.x = 65.757;
Camera.main.transform.position.z = 22.58412;
Camera.main.transform.rotation = Quaternion.Euler(0,270,0);
CameraFpos = false;
CameraRpos = true;
}
//Camera Right Spin from CameraRpos
if (CameraRpos && Input.GetButton("Right"))
{
Camera.main.transform.position.x = 18.0817;
Camera.main.transform.position.z = 69.14645;
Camera.main.transform.rotation = Quaternion.Euler(0,180,0);
CameraRpos = false;
CameraBpos = true;
}
//Camera Right Spin from CameraBpos
if (CameraBpos && Input.GetButton("Right"))
{
Camera.main.transform.position.x = -32.8335;
Camera.main.transform.position.z = 22.04136;
Camera.main.transform.rotation = Quaternion.Euler(0,90,0);
CameraBpos = false;
CameraLpos = true;
}
//Camera Right Spin from CameraLpos
if (CameraLpos && Input.GetButton("Right"))
{
Camera.main.transform.position.x = 18.9608;
Camera.main.transform.position.z = -25.7975;
Camera.main.transform.rotation = Quaternion.Euler(0,0,0);
CameraLpos = false;
CameraFpos = true;
}
}
Answer by AlucardJay · Feb 27, 2013 at 05:55 PM
Look at how your logic is organized.
if (CameraFpos && Input.GetButton("Right")) is true, then CameraRpos is set to true;
so then the next conditional becomes true : if (CameraRpos && Input.GetButton("Right"))
Even if you use Input.GetButtonDown, this is still recognized as it is still the same frame. So basically every if conditional in your script becomes true. The quick way to fix this is to use else if
Another consideration is you're checking for an input in every conditional. How about just check for the button first, if no button then nothing else needs to execute.
Finally, why is this all in FixedUpdate ? This is meant for physics steps, and as it runs at a different framerate to Update (and sometimes slower than Update depending on the time step or computing power). Also you should put as little code as possible in FixedUpdate. My advice for any camera movement is to apply it in LateUpdate, then everything else moves first, then the camera moves, this reduces shuddering/stuttering.
Here's another structure :
if ( Input.GetButtonDown( "Right" ) )
{
if ( CameraFpos )
{
//
}
else if ( CameraRpos )
{
//
}
else if ( CameraBpos )
{
//
}
else if ( CameraLpos )
{
//
}
}
now having said all that, I would recommend you use an enum for your camera state, and a switch-case depending on that state. Search enum, switch (switch case), and state engine (state machine).
http://answers.unity3d.com/questions/41108/switch-case-with-enum.html
Your answer
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
Change position of camera on scene load? 1 Answer
When switching camera position... 1 Answer
Aircraft wing position 0 Answers