# curve of animation in C#

I think that the animation curves in Unity lack the fact of being configurable because they are only chosen with a certain type (no sinusoidal function) and adjustable with handles.

How in C # create an animation (tanslation, rotation, scale) so as, for a selected mesh, to introduce parameters of a chosen mathematical function and any corresponding, if it is a translation for example, to x_position, y_position and z_position in function of the time variable?

thank you !

What if I now need to define time intervals? for example:

a function f1 on [0.20]

a function f2 on [20.50]

The most difficult is to convert by the correct Time.time factor so that the real time varies well in seconds within the limits defined by each of these intervals.

thanks

• You may find it's more efficient to just evaluate your selected function and convert it into equivalent Bezier curves so the existing animation system can chew through it all uniformly, rather than running a custom math expression for each parameter every frame. Commented Jan 8, 2020 at 22:13
• @DMGregory you are right, but there is also a case when you need the highest precision possible. As I understand AnimationCurve right now, with this approach it would consist of multiple points on path, value between some 2 points might not be the correct value at that t for the function. I can hardly think of a situation when it would be needed in AnimationCurve, but the animation curve would be a nice thing to display the function visually. If we take into account custom functions that aren't predefined standards like Mathf.Sin, and they are hard to visualise in the head. Commented Jan 9, 2020 at 9:36
• You're limited to float precision either way, so I don't agree that using a custom function gets more precision. It's just a matter of how densely you sprinkle your keyframes to get a closer and closer polynomial approximation to the target function. Commented Jan 9, 2020 at 12:00
• Looking at how everyone else has answered I might be missing the point entirely, but if you are looking to do something at certain points during an animation, why not use Animation Events? Commented Sep 30, 2021 at 16:07

You find the basic trigonometric functions in the class Mathf. If you want something to move in a trigonometric function over time, you just need to feed the current Time.time into the function.

For example, if you want an object to oscillate on the y axis in a sine curve, you would add this behaviour:

using UnityEngine;

public class SineMovementBehaviour : MonoBehaviour
{
void Update()
{
Vector3 pos = transform.position;
pos.y = Mathf.Sin(Time.time);
transform.position = pos;
}
}

• If you don't like the frequency, multiply Time.time with a constant.
• If you don't like the phase, add a constant to Time.time.
• If you don't like the amplitude, multiply the result with a constant.

Mathf.Sin(Time.time * frequency + phase) * amplitude;