- Home /
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.
Answer by AlucardJay · Oct 03, 2012 at 02:25 PM
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 ..." );
return;
}
var lastframe : Keyframe = myCurve[ myCurve.length - 1 ];
lastKeyTime = lastframe.time;
Debug.Log( " lastframe.time = " + lastframe.time );
Debug.Log( " lastframe.value = " + lastframe.value );
}
Thank you i could get the time and value of the lastframe, but i am getting IndexOutOfRangeException with that script. How can it be fixed ?
Did you actually put any keyframes (or a preset curve) in myCurve? This is just a bare example, so I havn't included a check for if there is no curve or keyframes. This script is tested and working (it works even with just one keyframe), make sure you actually have a curve in the window =]
have edited my answer to check if there is no curve or keyframes present*
it still won't work as intended :( I have created a curve in the inspector and applied it to my prefab so unless that the problem, yes my projectile got a curve.
Now i am realizing that the first instance of the object actually runs the curve just fine(but still wont destroy itself) But every instance of this object is at the same value as the first spawned one regardless of when it spawned. ie: it takes 2 second to reach maximum light.range, if a projectile spawn after that, light.range are set to maxrange.
$$anonymous$$y code is messy now since i have over 200 code line of tests commented here and there, anyone can make a quick code for it so i better understand how it should be done ? A light, that, on spawn, change its light.range over 2 second and then destroy itself.
I just scrolled up to check your code, didn't see any but realized that you are coding in C#. Sorry about that, but it seems you are more than capable of converting it (there are no compile errors so that is not the issue). Have you tried just running a C# version of my answer, add a key up input check in Update to run the method, then modify the curve and see if at least the last key is being read and it is returning the time of the last key?
$$anonymous$$y last guess is to check the prefab in the project window has the curve, all instantiated instances of that prefab should inherit the curve, I shall test further to investigate (having not instantiated an object with a curve myself). In the meantime, I think I understand your request and shall look into writing a script that can do that.
While I am happy to, you should also consider the design of this object. Do you know how long it lives for? If yes, then simply call a destroy with a delay on start, then make sure your curve is profiled so that the intensity falls to or below zero before the delay set in the destroy. For alot of instantiated objects, this calculate definite time periods for events could save valuable CPU loading =]
Also I just noticed you said you are learning the curves, maybe some info on my answer here may help with that : http://answers.unity3d.com/questions/320689/derive-an-equation-from-a-graph.html
Thank you for your answers ! No problem for converting Js to C# i can manage :)
I just cleaned everything and your code does work, ,it still gives me OutOfRange Error but it is working... might be me converting to C# that forgot some C# rule. It was my other problem that lead me to thing it didnt work, (every instance of this object are set to the same time) i created a new thread for this one in case no solution can be found here. http://answers.unity3d.com/questions/326912/projectiles-with-animated-light-are-all-animating.html
awaiting your script :)
Answer by Zerot · Oct 03, 2012 at 02:03 PM
You need to use http://docs.unity3d.com/Documentation/ScriptReference/AnimationCurve.Index_operator.html 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()`
Thank you for the answer, i am messing around with the index, but i still cannot get it to do something when it reaches the end.. Could you post a "working" piece of code ?
I am also getting out of bound error
Answer by Hi_ImTemmy · Oct 21, 2021 at 07:36 PM
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()
{
FindLastKey();
}
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
}
else
{
// End of animation curve!
}
}
}
Your answer
Follow this Question
Related Questions
How to use animation curve to drive object scale by distance instead of time? 1 Answer
How do i redraw an entire curve with the mouse (as opposed to one single keyframe redraw)? 0 Answers
Get value from a cruve like in AnimationCurve? 1 Answer
How can I do Flexible curved car or car on curved force in Unity 5? 0 Answers
Is there an easier way to edit multiple Animation Curves together? 0 Answers