Using WaitForSeconds with Movement

Basically I have a ship that is rotating, and the player can add a force to move it in a certain direction for a few seconds. It moves straight in whatever direction it is facing indefinitely, but I need it to start spinning again after a few seconds (so the player can choose a new direction). Here’s the code I have for rotation and movement (this is a 2D game), how do I use WaitForSeconds to initiate the spinning again?

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

public class PlayerController : MonoBehaviour
{
public float rotateSpeed = 100; // desired rotation speed
private float currentRotateSpeed; // current rotation speed
public float moveSpeed = 100; // move speed

public GameObject ship;

// Update is called once per frame
void Update()
{
    // check for movement, if moving, don't rotate
    if (ship.GetComponent<Rigidbody2D>().velocity.magnitude > 0.2f) // or 0.0f :). Just check for zero movement....
    {
        currentRotateSpeed = 0;
    }
    else
    {
        currentRotateSpeed = rotateSpeed;
        
    }

    transform.Rotate(new Vector3(0, 0, 1) * Time.deltaTime * currentRotateSpeed); // rotate on z axis

    if (Input.GetKeyDown(KeyCode.Space))
    {

        ship.GetComponent<Rigidbody2D>().AddRelativeForce(new Vector2(0, moveSpeed *100)); // add force on objects y axis
    }
}

}

Thanks

Use a coroutine…

After you finish and want to wait for a couple of seconds, do something like this …

 StartCoroutine(StartSpin());

then add the following method…

   IEnumerator StartSpin()
    {
        yield return new WaitForSeconds(2); //change the 2, to however long you want to wait

        //Start your spin routine here
    }

Hope this helps,
-Larry

@Larry-Dietz

{
public float rotateSpeed = 100; // desired rotation speed
private float currentRotateSpeed; // current rotation speed
public float moveSpeed = 100; // move speed

public GameObject ship;

//Update is called once per frame

void Update()
{

    //check for movement, if moving, don't rotate
    if (ship.GetComponent<Rigidbody2D>().velocity.magnitude > 0.2f) // or 0.0f :). Just check for zero movement....
    {
        currentRotateSpeed = 0;
        StartCoroutine(StartSpin());
    }

    else
    {
        currentRotateSpeed = rotateSpeed;

    }

    transform.Rotate(new Vector3(0, 0, 1) * Time.deltaTime * currentRotateSpeed); // rotate on z axis

    if (Input.GetKeyDown(KeyCode.Space))
    {

        ship.GetComponent<Rigidbody2D>().AddRelativeForce(new Vector2(0, moveSpeed * 100)); // add force on objects y axis
    }

}

IEnumerator StartSpin()
{
    yield return new WaitForSeconds(2);
    currentRotateSpeed = rotateSpeed;

}

}

Current Code^^^^^

I don’t really know how to code, so a lot of this is piecemeal. The spinning unfortunately stops after space is clicked and doesn’t resume, so it isn’t really working so far. Do you see anything that may be causing this?

Thanks