Attention |
Topic was automatically imported from the old Question2Answer platform. | |

Asked By |
SiL3NC3 |

Is there a way to use lerp() function with a KinematicBody2D within a C# script?

There was nothing found in the docs.

Attention |
Topic was automatically imported from the old Question2Answer platform. | |

Asked By |
SiL3NC3 |

Is there a way to use lerp() function with a KinematicBody2D within a C# script?

There was nothing found in the docs.

Reply From: |
MiltonVines |

9

Linear interpolation (lerp) is actually a pretty easy function to implement. The equation is

```
float Lerp(float firstFloat, float secondFloat, float by)
{
return firstFloat * (1 - by) + secondFloat * by;
}
```

A higher order Lerp just wraps lower order lerps:

```
Vector2 Lerp(Vector2 firstVector, Vector2 secondVector, float by)
{
float retX = Lerp(firstVector.x, secondVector.x, by);
float retY = Lerp(firstVector.y, secondVector.y, by);
return new Vector2(retX, retY);
}
```

The DirectX SDK has all manner of math functions like Unity, but that’s a lot of overhead to bring in just for Lerp. You’re probably best off just implementing your own.

Thank you.

Why is this then not simply implemented in Godot to provide it… ?!

Are there more stuff, which needs to be implemented by our self, which is missing in C# implementation instead of GDScript?

These issues should be also documented or better implemented in any way to increase equality of the C# implementation.

In that case I also missing:

- the GDScript support for directly accessing the sub-nodes with “$variable”
- better preloading support for C#

Should I place these requests at github?

SiL3NC3 | 2020-11-26 08:48

Use Mathf.Lerp

cihad | 2021-04-03 18:13

Reply From: |
luiscla27 |

If you dont want to implement your own, Godot implemented `Mathf`

```
Mathf.Lerp(firstFloat, secondFloat, thirtdFloat);
```

I was looking for `clamp`

and `lerp`

functions when I found this, please refer to the following reddit thread:

https://www.reddit.com/r/godot/comments/eknqs2/c_clamp_function_name/

This is a quick quote from it:

Mathf and GD are from Godot library, only available when using Godot.

Mathf is for math functions, and GD for non-math functions. Mathf is

optimised for fast approximate results (not for high precision

computations).

1 Like

I have this in my library:

```
/// <summary>
/// Linear interpolation between <c>First</c> and <c>Second</c> by <c>Amount</c>
/// </summary>
public static float Lerp(float First, float Second, float Amount)
{
return First * (1 - Amount) + Second * Amount;
}
/// <summary>
/// Linear interpolation between <c>First</c> and <c>Second</c> by <c>Amount</c>
/// </summary>
public static Vector3 Lerp(Vector3 First, Vector3 Second, float Amount)
{
float retX = Lerp(First.X, Second.X, Amount);
float retY = Lerp(First.Y, Second.Y, Amount);
float retZ = Lerp(First.Z, Second.Z, Amount);
return new Vector3(retX, retY, retZ);
}
/// <summary>
/// Linear interpolation between <c>First</c> and <c>Second</c> by <c>Amount</c>
/// </summary>
public static Vector2 Lerp(Vector2 First, Vector2 Second, float Amount)
{
float retX = Lerp(First.X, Second.X, Amount);
float retY = Lerp(First.Y, Second.Y, Amount);
return new Vector2(retX, retY);
}
```

But douing it THIS way is Extra non-performant. If anyone more qualified in c# can turn this into System.Numerics.Vector.Lerp i would say Thankyou. I’ll probably do it myself one day, but not today.

1 Like