The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+2 votes

Is there a way to use lerp() function with a KinematicBody2D within a C# script?
There was nothing found in the docs.

in Engine by (21 points)

2 Answers

+1 vote

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.

by (22 points)

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?

Use Mathf.Lerp

+3 votes

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).

by (20 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.