Transform rotation and position on key input: rotation only working the first time

Hi,

I want to rotate and move a game object to a set position and rotation.
The transform.position is working fine, but the rotation only works the first time I press the q and w key, but after that it doesn’t rotate the object to the correct position.

I just can’t work out what I’m doing wrong - I thought maybe I wasn’t resetting a value correctly, but I can’t see what I’m missing.

So I want the object to rotate to 9 degrees on the x axis every time q is pressed, and 45 degrees on the x axis when w is pressed.

Does anyone have any ideas on what I’m doing wrong?

Best,
Laurien

Edit: updated script

 using UnityEngine;
using System.Collections;

public class MovementRotationCoroutine : MonoBehaviour {

	public GameObject cameraMain;

	private float degree;
	private float angle;




	void Update () {

		if (Input.GetKeyDown("q"))
		{
			degree = 9f;
			StartCoroutine ( MoveToPositionPersp (new Vector3(0,0.5f,-5.5f), 2f, 2f));
		}


		if (Input.GetKeyDown("w"))
		{
			degree = 45f;
			StartCoroutine ( MoveToPositionOrtho (new Vector3(0,5.5f,0), 2f, 2f));
		}
	}




	public IEnumerator MoveToPositionOrtho(Vector3 position, float timeToMove, float waitTime)
	{
		yield return new WaitForSeconds(waitTime);


		var currentPos = transform.position;
		var currentPosRotate = transform.rotation;
		var t = 0f;
	

		while(t < 1)
		{
			t += Time.deltaTime / timeToMove;

			transform.position = Vector3.Lerp(currentPos, position, t);

			angle = Mathf.LerpAngle(transform.rotation.x, degree, t);
			transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(degree, 0, 0), t);

			yield return null;
		}
	}





		public IEnumerator MoveToPositionPersp(Vector3 position, float timeToMove, float waitTime)
		{
			yield return new WaitForSeconds(waitTime);

			var currentPos = transform.position;
			var currentPosRotate = transform.rotation;
			var t = 0f;
			
			
			while(t < 1)
			{
				t += Time.deltaTime / timeToMove;
				transform.position = Vector3.Lerp(currentPos, position, t);
				
				angle = Mathf.LerpAngle(transform.rotation.x, degree, t);
				transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(degree, 0, 0), t);
				
				yield return null;
			}
	}
	
}

Save your coroutines in an ienumerator. When calling one or the other, stop the coroutines, then start up the new one:

IEnumerator _rotating;

If (_rotating != null) StopCoroutine(_rotating);
StartCoroutine(_rotating = MoveToPositionPers(‘args’));

may you help me with my code?

what I want to do is make it make my rotations get stored in a quaternion and then change the quaternion to look up and down when the left and right arrow keys are pressed. However I am fairly new to quaternions and unity in general so I have no Idea how to do this.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class look : MonoBehaviour
{

[SerializeField]
KeyCode up;
[SerializeField]
KeyCode down;

// Update is called once per frame
void Update()
{
    if (Input.GetKey(up))
    {
        transform.Rotate(-2f, 0f, 0f);
        
    }
    if (Input.GetKey(down))
    {
        transform.Rotate(2f, 0f, 0f);
        
    }
}

}

please help me with integrating the quaternions into my code.