Slowly increase motor.force

Hello!

I created a fairly simple script to add motor.force/velocity to a gameobject.
But, I want it to increase it over time and not instant.

But i’m not sure on how to achieve this.
Still fairly new to C#.

If anyone can help me with this, that will be highly appreciated.

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

public class ArmMotor : MonoBehaviour
{
    private HingeJoint _hingeJoint;
    // Start is called before the first frame update
    void Start()
    {
        _hingeJoint = GetComponent<HingeJoint>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown("e"))
        {
            JointMotor motor = _hingeJoint.motor;
            motor.force = 0;
            motor.targetVelocity = 0;
            _hingeJoint.motor = motor;
        }
        if (Input.GetKeyDown("d"))
        {
            JointMotor motor = _hingeJoint.motor;
            motor.force = 1500;
            motor.targetVelocity = 90000;
            _hingeJoint.motor = motor;
        }
    }
}

The easiest way to do this is probably just to add an amount multiplied by Time.deltaTime (which is the time between frames) when the button is held and subtracting in the same way when ‘e’ is held, and just checking to see if the value is greater or less than the desired value.

speed = 1;

if (motor.force <= 1500)
    motor.force += speed * Time.deltaTime;
else 
    motor.force = 1500;

The else at the end is just to make sure it doesn’t go over the destination value. Also just in case you’re unfamiliar, writing ‘motor. force += x’ is the same as writing ‘motor.force = motor.force + x’
You could also potentially use the function Mathf.Lerp (), but I think this way is the easiest method for what you want to do.