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.

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 :slight_smile:

Of course, the answer is accepted! Thank you, again! Now i’ll try to play a little with the clamp script :slight_smile: