0 votes


Godot 3.2.2 stable on macOS

I am trying to replicate the following motion from the Godot docs: https://docs.godotengine.org/en/latest/tutorials/math/interpolation.html
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!

in Engine by (586 points)

try self.position

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

2 Answers

+1 vote
Best answer

Instead of:

var mouse_pos = get_local_mouse_position()

I think you want this:

var mouse_pos = get_global_mouse_position()
by (12,280 points)
selected by

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.linearinterpolate((target.getglobal_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?

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

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.

0 votes
by (14 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 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.