Lerp direction not work in c#

Godot Version

4.2

Question

Hello, I’m creating a game in C# godot. I are creating a FPS controller but, to create a smooth movement, use a lerp and this get me a error. Sorry for my bad english.

image

You are looking for the method on Vector3, the one in Mathf isn’t for vectors, try direction.Lerp(...)

1 Like

The expression you entered GlobalTransform.Basis * new Vector3(input_dir.X, 0, input_dir.Y) gives back a Vector3 value. However the Mathf.Lerp function’s second argument (which is the place where you’ve entered the expression causing the error) must be a float, since the function itself returns a float.
Basically, you can’t use Mathf.Lerp to interpolate a Vector3 into another; Mathf.Lerp is only used to interpolate numerical values. Nevertheless, Godot has a Vector3 method called move_toward in GDScript that does what you’re wanting to do (cf Vector3 — Godot Engine (stable) documentation in English). The C# equivalent of this method is probably called MoveToward().

So I’d rewrite the line pushing the error as:

direction = direction.MoveToward(new Vector3(input_dir.X, 0.0, input_dir.Y), (float)delta * air_res).Normalized();

(Forgive me if I did any C# syntax errors, I usually code in GDScript.)

1 Like

Thanks, it works!!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.