0 votes

I am doing Lerp using below function

func _process(delta)
    t=delta*speed
    positionA=positionA.linear_interpolate(positionB,t)
   pass

above code giving straight line animation from Position A To B like below image
enter image description here
I want to achieve animation like below image
enter image description here

in Projects by (21 points)
edited by

1 Answer

0 votes
Best answer

First of all: the code you provided is not working! It's missing a colon in the first line, a var in the second line and the definition of speed, positionA and positionB. Also your time-variable t does not increase, but is (more or less) constant.

Here is a complete and working script that you can attach to a Sprite-node:

extends Sprite

var positionA = Vector2(200, 500)
var positionB = Vector2(500, 200)

var t = 0.0
var duration = 5.0

func _process(delta):
    t += delta / duration
    position = positionA.linear_interpolate(positionB, min(t, 1.0))

If you don't want to move from positionA to positionB in a straight line, you can't linearly interpolate the position - that should be obvious from the name! However, you can define a third position and draw a quadratic Bezier curve:

extends Sprite

var positionA = Vector2(200, 500)
var positionB = Vector2(500, 200)
var positionC = Vector2(200, 200)

var t = 0.0
var duration = 5.0

func _process(delta):
    t += delta / duration
    var q0 = positionA.linear_interpolate(positionC, min(t, 1.0))
    var q1 = positionC.linear_interpolate(positionB, min(t, 1.0))
    position = q0.linear_interpolate(q1, min(t, 1.0))
by (10,057 points)
selected by
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.

Categories