Linear interpolation

Hi guys, need help.
I want to do linear interpolation. There is an enemy find function that gives us the closest enemy - nearest_target(). And there is also a start - start_position. I asked GPT for the code for linear interpolation and when I run it, it gives an error: "Invalid operands ‘Nil’ and ‘Vector2’ in operator ‘-’ "
How can I fix this error?

I think that we need to remove normalized or pass the data of this function to some kind of variable.

Godot’s vector classes (Vector2/Vector3) have a built-in interpolation function.

Vector2.lerp()
Vector3.lerp()

With regards to your error, it’s likely a result of using target_position() without checking whether its result is Nil.

I think from this script you want to move towards the closest enemy, which actually does not use linear interpolation.

Does “Enemyfindtest” exist? If so it .nearest_target() is returning null, it’s also being called twice which must be bad for performance. Could you paste the script attached to this node (if it exists)?

Your start_position variable won’t change is that intentional? move_and_slide() also will not change as velocity is not set, did you mean to use velocity instead of movement?

1 Like