Can Animation Curves be used to control variables?
I have 2 basic float variables; distance and damage. The damage variable changes based on the distance variable. At the moment I have several damage variables due to the number of times the damage changes over distance (eg. 50 dmg between 0-20m, 30 dmg between 20-40m etc).
I noticed that the standard asset FPS controller uses an animation curve to control its slope curve modifier. I have never used the animation curve variable type, but I'm wondering if it is possible to use it for my particular case? For example, if I could make the y axis = damage, and the x axis = distance then I could create a single animation curve that represents this instead of having 6 separate variables. Is this possible with animation curves?
I have had a look at the FPS controller script but I do not quite understand how it is using the animation curve to read values from it and translating those into logic. I think it might involve the Evaluate function but there isn't an example in the docs.
Can this be done?
Answer by Prefab · Jun 27, 2016 at 06:00 AM
Ok I have managed to work this out. It is a bit confusing and isn't properly explained anywhere but here goes. First you need to declare an AnimationCurve variable. Then open the curve editor of this variable and use the Y axes for damage and the X axes for distance. Create your keyframes depending on how you want the damage to change over distance. Then you simply need to make the damage equal to the AnimationCurve evaluated. Here is a snippet to demonstrate this:
distanceTravelled = Vector3.Distance(firingPosition, transform.position);
damage = damageCurve.Evaluate(distanceTravelled);
This is actually very powerful. I am surprised that it isn't documented better anywhere.
I need to achieve the same thing, thanks for answering your question :)
I want to achieve the same thing and this really helped me out. Thing is, the numbers I'm working with are larger than 0-1. What I'm doing is calculating air density, with the altitude range being 0-3500 meters. How can I rescale the curve to fit?
If you want to convert a 0-1 value (from here on curveValue) to some other scale, simply multiply it. This is your case:
Case: 0-3500
convertedValue = curveValue * 3500;
Since 0*3500 equals 0 and 1*3500 equals 3500 every in-between value will map from 0-3500.
If you need saaaay... -100-500, you could write it like this:
convertedValue = curveValue * 600 - 100;
All in all, there is no reason to modify the curve, you simply modify it's output. The rest is basic math.
This really help me as starting point to do what i want thanks
Answer by Arclite83 · Jul 16, 2021 at 03:04 PM
Personal tip: normalize your curves. It's far easier to do the math if X and Y are both 0-1, and your multipliers are elsewhere (other editor variables, probably, but just as easily coming from completely different systems).