WaitForSeconds doesn't work apart for the first time

I’ve got a script that moves my camera 90 degrees around the player every time the right arrow or left arrow is pressed.

I want it such that if a key has been pressed then the camera rotates around the player but while the camera is rotating you are not allowed to press the rotation button again until after the movement of the camera has completed.

The problem I’m having is that I’ve only partially managed to solve this with the code below. I press the key once, and multiple times while the camera is moving. The first time the camera moves it works. It doesn’t allow any of the key presses to go through.

Every other time it doesn’t work. It allows the multiple key presses and the camera will only stop moving once the entire amount of key presses have run out.

Can you please take a look at my code and tell me what it is I’m doing wrong?

using UnityEngine;
using System.Collections;

public class CameraMovement : MonoBehaviour 
{
	private GameObject targetObject;
	private float targetAngle = 0;
	const float rotationAmount = 1.5f;
	bool keyPress = false;

	void Start()
	{
		targetObject = GameObject.FindGameObjectWithTag("Player");
	}

	void Update()
	{
		if (!keyPress)
		{
			if(Input.GetKeyDown(KeyCode.LeftArrow)) 
			{
				targetAngle -= 90.0f;
			}
			
			else if(Input.GetKeyDown(KeyCode.RightArrow))
			{
				targetAngle += 90.0f;
			}
		}
		if (targetAngle != 0) 
		{
			Rotate();
			StartCoroutine("StopKeyPress");
		}
	}

	IEnumerator StopKeyPress()
	{
		if (!keyPress) 
		{
			keyPress = true;
			if (keyPress) 
			{
				while(keyPress)
				{
					yield return new WaitForSeconds(2);
				}
			}
		}

		keyPress = false;
	}
	
	void Rotate()
	{	
		if (targetAngle > 0)
		{
			transform.RotateAround(targetObject.transform.position, Vector3.up, -rotationAmount);
			targetAngle -= rotationAmount;
		}

		else if(targetAngle < 0)
		{
			transform.RotateAround(targetObject.transform.position, Vector3.up, rotationAmount);
			targetAngle += rotationAmount;
		}
	}
}

Your approach to this problem seems a little weird to me, but the coroutine is downright wrong. This should be all you need:

IEnumerator StopKeyPress()
{
  if (keyPress) yield break;
  keyPress = true;
  yield return new WaitForSeconds(2);
  keyPress = false;
  yield break;
}

Another (maybe more readable) solution

Holding down the L/R keys will give continuous rotation

void Update()
{
	if( !keyPress )
	{
		if( Input.GetKey( KeyCode.LeftArrow ) )
		{
			targetAngle -= 90.0f;
		}

		else if( Input.GetKey( KeyCode.RightArrow ) )
		{
			targetAngle += 90.0f;
		}
	}
	if( targetAngle != 0 )
	{
		if( !keyPress )
		{
			StartCoroutine( "StopKeyPress" );
		}
		keyPress = true;
		Rotate();
	}
}

IEnumerator StopKeyPress()
{
	while( targetAngle != 0 )
	{
		yield return new WaitForSeconds( 0.01f );
	}
	keyPress = false;
}