+1 vote

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 (20 points)

2 Answers

+1 vote


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

+1 vote

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:

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

by (16 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 webmaster@godotengine.org with your username.