Help With Rotation!

I’m trying to get my camera to rotate once per KeyDown and this is the code I have so far

 if (Input.GetKey(KeyCode.Space)) 
 {
     transform.Rotate(0,180,0 * Time.deltaTime); 
 }

What’s happening is it will rotate between 0 and 180 continuously per press unless I tap Space really quickly.
What can I do to make sure its only one 180 degree rotation per key press?
Also, I’m hoping to implement FPS camera movement in between button presses that are clamped to a small window.
I’m brand new to Unity and working on tutorials but something still isn’t clicking on how to manage rotation.

Any help would be greatly appreciated!

The Transform.Rotate() method performs the entire rotation all in one frame.

Any reason you’re making Yet Another Camera Controller in the year 2025??

Camera stuff is pretty tricky… I hear all the Kool Kids are using Cinemachine from the Unity Package Manager.

There’s even a dedicated Camera / Cinemachine area: see left panel.

If you insist on making your own camera controller, do not fiddle with camera rotation.

The simplest way to do it is to think in terms of two Vector3 points in space:

  1. where the camera is LOCATED
  2. what the camera is LOOKING at
private Vector3 WhereMyCameraIsLocated;
private Vector3 WhatMyCameraIsLookingAt;

void LateUpdate()
{
  cam.transform.position = WhereMyCameraIsLocated;
  cam.transform.LookAt( WhatMyCameraIsLookingAt);
}

Then you just need to update the above two points based on your GameObjects, no need to fiddle with rotations. As long as you move those positions smoothly, the camera will be nice and smooth as well, both positionally and rotationally.

I’m a straight-up beginner so I’m following YT and Unity tutorials and directly scripting Cameras is the only system I know so far. I’ll check out Cinemachine tuts though if it will soothe my frustrations.

However, you say in the example to ‘update the above two points’ but I don’t see any values to manipulate/update. Can you explain a little more how this script works?

Your code is responsible for moving the two Vector3s however you want to, the code above would handle positioning and aligning the camera.

I think I understand.

Do I add this to the code I have already? Or replace what I have with this?
And what library would i find ‘cam’ in?

If you’re asking these questions then just go with this:

But be VERY SURE you do tutorials correctly, or you will NOT be soothed:

Two steps to tutorials and / or example code:

  1. do them perfectly, to the letter (zero typos, including punctuation and capitalization)
  2. stop and understand each step to understand what is going on.

If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.

Step #2 is particularly critical when learning. If you don’t want to do Step #2, just ask someone else to do the whole game for you.

Like this guy:

Imphenzia: How Did I Learn To Make Games: