How to rotate an object graduallly using key?

So I want to write a code where if I press a key the object rotates at an angle gradually depending on how long i press the key and goes back to the same position after the key is not pressed. Will Quartenions and lerp work here? NEED HELP

Is there a “final” angle that it moves towards? If I hold the button down for a long time, will it just keep rotating forever? Or does it reach some limit and stop (even before releasing the button)?

Quaternions are how Unity handles rotation, so of course they will be involved.

yes there is a limit

Ok so pretty simple:

  • Use Input.GetKeyDown() to detect when the user starts holding the key down and set a boolean indicating that rotation has begun, and save the current rotation so we can revert to it later.
  • As long as rotation boolean is true, in Update() you can use Quaternion.MoveTowards(transform.rotation, targetRotation, Time.deltaTime * RotationSpeed)
  • Use Input.GetKeyDown() to detect when the key has been released, then you can set rotation = false, and set the rotation back to the original rotation.

So I tried your method for only the left rotation and wrote this

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

public class FlightMode : MonoBehaviour
{
   
    public Rigidbody rb;
    public Camera c1;
  
    public float rotatespeed = 30f;
    private Transform target;
  
    void Start()
    {
       
           
        rb.useGravity = false;
       
       
    }

    // Update is called once per frame
    void Update()
    {   
       
       
        Vector3 movecameraTo= transform.position - transform.right * 6f + transform.up * 1.5f;
        c1.transform.position = movecameraTo;
        c1.transform.LookAt(transform.position);
       
       
       
       
       
       
        transform.position += transform.right *Time.deltaTime*90f;
        if (Input.GetKey(KeyCode.A))
        {
            transform.position += transform.forward * 90f * Time.deltaTime;
           
            Quaternion.RotateTowards(transform.rotation, target.rotation = Quaternion.Euler(36.55f, 90.262f, -0.156f), Time.deltaTime * rotatespeed);
           
        }
        if (Input.GetKey(KeyCode.D))
        {
            transform.position += (-transform.forward * 90f * Time.deltaTime);
           
        }
        if (Input.GetKey(KeyCode.W))
        {
            transform.position += transform.up * 90f * Time.deltaTime;
       
        }
        if (Input.GetKey(KeyCode.S))
        {
            transform.position += (-transform.up * 90f * Time.deltaTime);
           
        }


        if (rb.position.y < -90f)
        {
            FindObjectOfType<GM>().EndGame();
        }
    }
}

But it doesn’t work.