coroutine not working second time

I have a simple script for a third person camera to rotate 90° once a button is pressed. I use a coroutine to rotate the camera here, but for some reason, the second time the coroutine runs, it won’t stop even though transform.rotation.eulerangles.yis equal torotateTowards

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

public class CameraBehaviour : MonoBehaviour
{
    public float rotationSpeed;
    public KeyCode rotateRight, rotateLeft;
  
    private bool isRotating = false;
    private float rotatingTowards;
    private Coroutine rotationRoutine = null;

    // Update is called once per frame
    void Update()
    {
        rotate();
    }

    void rotate()
    {

        if (!isRotating)
        {
            if (Input.GetKeyDown(rotateRight))
            {
                rotatingTowards = transform.rotation.eulerAngles.y - 90;

                rotationRoutine = StartCoroutine(rotate(rotatingTowards));
                isRotating = true;
            }
            if (Input.GetKeyDown(rotateLeft))
            {
                rotatingTowards = transform.rotation.eulerAngles.y + 90;

                rotationRoutine = StartCoroutine(rotate(rotatingTowards));
                isRotating = true;
            }
        } else
        {
            Debug.Log("rot = " + transform.rotation.eulerAngles.y + " goal = " + rotatingTowards);

            if (transform.rotation.eulerAngles.y == rotatingTowards)
            {
                Debug.Log("stopping coroutine");

                StopCoroutine(rotationRoutine);
                isRotating = false;
            }
        }
    }

    private IEnumerator rotate(float towards)
    {

        while (transform.rotation.eulerAngles.y != towards)
        {
            transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(new Vector3(0, towards, 0)), rotationSpeed);

            yield return new WaitForFixedUpdate();
        }
    }
}

If you add a debug log here you’ll see what’s going on:

 private IEnumerator rotate(float towards)
    {
        while (transform.rotation.eulerAngles.y != towards)
        {
            transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(new Vector3(0, towards, 0)), rotationSpeed);

            yield return new WaitForFixedUpdate();
            Debug.Log(transform.rotation.eulerAngles.y);
        }
    }

I don’t know the exact specifics of your game, but this rotation script is very convoluted and over complex. With a bit of details on what you’re trying to achieve I could help you re-write it.

Thank you for your reply.

If I add your line, I get the same value as the debug in Rotate().

As for what I want to make. I have an isometric game, and I’d like the player to be able to rotate the camera 90° if they want to see the game from another angle.

Alright, try this:

private Vector3 _rotationTarget;
    [SerializeField]private float _rotationSpeed = 1;
    public void Update() {
        if (Input.GetMouseButtonDown(0)) _rotationTarget += new Vector3(0, 90, 0);
        transform.rotation = Quaternion.RotateTowards(transform.rotation,Quaternion.Euler(_rotationTarget), _rotationSpeed);
    }
1 Like

Well that was surprisingly a lot more simple than I would’ve ever made it.

Thank you very much!

Honestly, I feel like coroutines just complicate a lot of objectives. They have their place… but only barely.