AnimationCurve, how can i know when the end of the curve has been reached?

In my little test project, i decided that I want to lern how to use animationcurve.
There is not much documentation but i managed to have an object move using a curve i created.
Unfortunately, the object continue to move into infinity since i dont know how to tell it to stop.

The problem is, i have no idea how to specify a lengh of time for my curve and tell my script to stop running when it reached it.
Strangely, curve.length tells me the number of key, which is not what i want.

Let’s say i want to animate a light.range, with a min intensity and a max intensity, over a specified time, using a curve how would i do that in C#?

I searched a lot and i couldn’t find an answer that can be apply to my situation.

suggested steps :

find the time of the last keyframe

check if foobar > last keyframe . time

if so then stop, else evaluate

Here’s how to find the time and value for the last keyframe :

public var myCurve : AnimationCurve;
var lastKeyTime : float;

function FindLastKeyframe()
    // CHECK if there is NO CURVE
    if ( myCurve.length == 0 )
        Debug.Log( "NO CURVE ..." );
    var lastframe : Keyframe = myCurve[ myCurve.length - 1 ];
    lastKeyTime = lastframe.time;
    Debug.Log( " lastframe.time = " + lastframe.time );
    Debug.Log( " lastframe.value = " + lastframe.value );

You need to use in combination with length. You get the last key by getting the one at index “length-1” and then you check its time field and compare it to your internal time field.

So, if (curve[curve.length-1].time>t) Stop()

I just hit this very issue today.

My solution in C# which I believe is the same as the steps outlined:

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

public class CurveQues : MonoBehaviour
    public AnimationCurve curve;

    private float curveTime;
    private float lastKeyTime;

    void Start()

    void FindLastKey()
        //Get the last keyframe and log it's time
        Keyframe lastKey = curve[curve.length - 1];
        lastKeyTime = lastKey.time;

    void Update()
        curveTime += Time.deltaTime;

        if (curveTime < lastKeyTime)
            // Do stuff like curve evaluation
            // End of animation curve!