Question About Linear Interpolation and Vector2s

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By scrubswithnosleeves

Hello!

Godot 3.2.2 stable on macOS

I am trying to replicate the following motion from the Godot docs: Interpolation — Godot Engine (latest) documentation in English
Lerp gif from Godot docs

here is the code they provide:

const FOLLOW_SPEED = 4.0

func _physics_process(delta):
    var mouse_pos = get_local_mouse_position()

    $Sprite.position = $Sprite.position.linear_interpolate(mouse_pos, delta * FOLLOW_SPEED)

This works as long as the scene is set up so that the .position your referencing is a child of the node that contains the script.

However, if I put the following code into a script in the Sprite node itself:

const FOLLOW_SPEED = 4.0

func _physics_process(delta):
    var mouse_pos = get_local_mouse_position()

    position = position.linear_interpolate(mouse_pos, delta * FOLLOW_SPEED)

I get the following odd movement:
My example output

It is also odd that when I print mouse_pos and position, they still say they are relatively equal to each other. Can anyone tell me what I am doing wrong or if this is a bug?

I was planning on using something like this to make an enemy that followed a certain distance behind the player, and I wanted to put the lerp movement code withing the enemy node script itself. I’m pretty comfortable with lerping, but I don’t get what is happening in this case.

Thanks in advance!

try self.position

sirAlexDev | 2020-08-31 19:30

Tried that :(. No change whatsoever. Also tried global_position.

scrubswithnosleeves | 2020-08-31 19:32

:bust_in_silhouette: Reply From: jgodfrey

Instead of:

var mouse_pos = get_local_mouse_position()

I think you want this:

var mouse_pos = get_global_mouse_position()

I thought the solution to this generic problem would solve my specific problem but it didn’t :(.

I wanted to interpolate the position of this enemy self to be 50px above the player target using the following code.
self.position = self.position.linear_interpolate((target.get_global_position() + Vector2(0,-50)), delta*4)

I changed target.get_position() to target.get_global_position(), as that worked in the follow mouse example, but no dice in my game. Here is the result of that code:
enter image description here

The above code is called after the enemy does that “dash” movement towards the player, and then he seems to interpolate towards the position he ends up at after the dash.

Any ideas?

scrubswithnosleeves | 2020-08-31 21:20

Hard to say without seeing more code, but have you tried changing the self.position references to self.global_position?

jgodfrey | 2020-08-31 21:28

lol figured it out. the error was elsewhere in my code. I had move_and_slide() in the main _physics_process() loop rather than the loop for the “dash” attack so it was still moving the body according to the last defined velocity from the “dash” attack.

scrubswithnosleeves | 2020-08-31 22:08

:bust_in_silhouette: Reply From: Nkpresh

try using this link instead Interpolation — Godot Engine (stable) documentation in English