Hi.
I need to find a function to move it smoothly between position.x 3
and position.x 9
:
positon.x = 3, rotation.y = 5
positon.x = 6, rotation.y = 15
positon.x = 9, rotation.y = 25
Hi.
I need to find a function to move it smoothly between position.x 3
and position.x 9
:
positon.x = 3, rotation.y = 5
positon.x = 6, rotation.y = 15
positon.x = 9, rotation.y = 25
Hi, there are multiple built in functions to move things slowly.
For transform.position (Vector3)
Vector3.MoveTowards
Vector3.Lerp
For transform.rotation (Quaternion)
Quaternion.Slerp
Quaternion.Lerp
On how to use methods just check google there are gazillion examples
I’ll just leave the code that I use for my own “smooth” movements here. A tip is to experiment with sin waves on desmos, if you want to make your own functions like I do.
public static float CubedExp(float value, float start, float end, float time)
{
float x = 2f * time - (time * 0.5f * Mathf.Pow(Mathf.Pow(Mathf.Log((value - start) / (end - start)), 2f), 1f / 6f) + time);
float xIncreased = Mathf.Clamp(x + Time.deltaTime, 0, time);
return (end - start) * Mathf.Pow(2.71828f, (2f * xIncreased / time - 2f) * (2f * xIncreased / time - 2f) * (2f * xIncreased / time - 2f)) + start;
}
public static float CubedExpByX(float x, float start, float end, float time)
{
float xIncreased = Mathf.Clamp(x + Time.deltaTime, 0, time);
return (end - start) * Mathf.Pow(2.71828f, (2f * xIncreased / time - 2f) * (2f * xIncreased / time - 2f) * (2f * xIncreased / time - 2f)) + start;
}
If you want to smoothly change position.x from 3 to 9 over a period of 4 seconds, do this:
position.x = CubedExp(position.x, 3f, 9f, 4f);
CubedExpByX basically means that instead of using the current value as the first input, you use the current time since start.