How to move object using Lerp with Arc Animation in 2D

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

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

:bust_in_silhouette: Reply From: njamster

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))