- Home /
How can i make the camera look up and down through keyboard keys?
I would like to set, for my First Person build, keys Q & E as camera command to make it look up and down, to a max 90°/-90°, plus a key to reset camera to base 0° position.
I'm new to programming but i actually can't find a guide about setting specific keys to each camera movement. All first person guides are made for mouse shooters and that's not what i'm making.
Answer by jackmw94 · Feb 01, 2021 at 10:48 AM
There are a few concepts you'll need to know for this lil bit of functionality:
Firstly, how to detect what inputs you're receiving. Unity has a new input system out but it's a little involved so given you're learning I'd advise sticking to the old system.
To check whether a key has been pressed this frame, you can use Input.GetKeyDown
and for whether it's being pressed and held Input.GetKey
. You pass this in a keycode which is a big ol enum full of all the different keys you could be querying, so to test whether Q or E are pressed:
bool qKeyIsPressed = Input.GetKey(KeyCode.Q);
bool eKeyIsPressed = Input.GetKey(KeyCode.E);
Now on to rotations. Anything to do with positioning, scaling or rotating in Unity is done via the transform. There is a 1-to-1 relationship between gameobjects and transforms, each has access to the other.
Rotations are naturally quaternions which are scary sounding but quite nice once you get to know them. You're probably used to dealing with rotations in euler angles - the 3 xyz variables you see in the transform - you can convert between these two.
Sooo to increase / decrease the pitch, you'll be adjusting the x euler angle. As an example, the following code will increase the pitch by 10 degrees:
var currEulerAngles = transform.eulerAngles;
currEulerAngles.x += 10f;
transform.rotation = Quaternion.Euler( currEulerAngles );
To run this every frame (at 60 frames per second) would spin you pretty fast so you can scale this by Time.deltaTime which is the time that's elapsed since last frame, this will keep the speed at a consistent value regardless of framerate. Also to scale the speed you can add in your own speed value as a serialized field that you can alter in the inspector.
if (qKeyIsPressed)
{
var currEulerAngles = transform.eulerAngles;
currEulerAngles.x += _speed * Time.deltaTime;
transform.rotation = Quaternion.Euler( currEulerAngles );
}
Combined with the code from above, this will mean that when it detects input from the Q key, we run the code that will increase the x euler angle by _speed (your own speed variable) each second. You can apply a similar but negative operation for the E key. Now we're getting somewhere.
To add in the maximum and minimum we just need to clamp the x euler angle between -90 and 90. After you have applied the upwards and downwards rotations, you can take the x euler angle and use Mathf.Clamp
to ensure it exists between -90 and 90.
Let me know how you get on this, I can provide more help if there's anything you don't understand or if you have issues when implementing :)
WOW. That was quick! And really usefull, thank you very much. It works!
if(Input.GetKey(KeyCode.Q)) {
var currEulerAngles = transform.eulerAngles;
currEulerAngles.x += speed * Time.deltaTime;
transform.rotation = Quaternion.Euler(currEulerAngles);
currEulerAngles.x = $$anonymous$$athf.Clamp(currEulerAngles.x, -70, 70);
}
if(Input.GetKey(KeyCode.E)) {
var currEulerAngles = transform.eulerAngles;
currEulerAngles.x -= speed * Time.deltaTime;
transform.rotation = Quaternion.Euler(currEulerAngles);
currEulerAngles.x = $$anonymous$$athf.Clamp(currEulerAngles.x, -70, 70);
}
With this build i can rotate the camera up and down but i still have an issue with the mathf.clamp for the max/$$anonymous$$ angle. I could not put it in the update section because the currEulerAngles.x is never mentioned in void Update. In the code above the max/$$anonymous$$ rotation in clamped indeed, but is set at -90/90 and i can't find a way to regulate it for other values.
The point at which we use that currEulerAngles is when we assign a value to transform.rotation, after that it's useless as it will go out of scope and any other value we set it to will be unused. Sooo we want to be clamping its value before we use it to get the rotation.
When you say "regulate" it, are you thinking of having it as a value in the inspector? Sounds kinda like your speed field - having your $$anonymous$$imum / maximum clamp values as serialized fields will allow you to change them in the inspector.
If you're thinking this code is a bit hard to read, you can do a bit of refactoring to reduce the number of lines here (if you're happy with it, you don't need to do this!) : all that needs to happen within the if statements is to adjust currEulerAngles.x, we can do everything else outside of this.
var currEulerAngles = transform.eulerAngles;
if (Input.GetKey(KeyCode.Q))
{
currEulerAngles.x += speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.E))
{
currEulerAngles -= speed * Time.deltaTime;
}
currEulerAngles.x = $$anonymous$$athf.Clamp(currEulerAngles.x, $$anonymous$$Pitch, maxPitch);
transform.rotation = Quaternion.Euler(currEulerAngles);
Just thought I'd show this for a bit of an example of how you can move these things around and get the same behaviour.
Hope this helps + feel free to ask more Qs. Can you accept my answer if you're happy with it? Thanks :)
Answer by Nekrell · Feb 02, 2021 at 12:11 PM
Of course, the answer is accepted! Thank you, again! Now i'll try to play a little with the clamp script :)
Your answer
Follow this Question
Related Questions
Jerky 3rd Person Camera Following Movement and Rotation 0 Answers
Screen is 2 cameras with each different properties. 1 Answer
camera movments fixed.. character controller without using character controller -.-' 2 Answers
Rotate Camera to the object 0 Answers
How to have free rotation camera? 1 Answer