Problem with interpolation 2D movement

Godot Version

Godot 4.2.1

Question

Hello, this is the first time I’m doing game development. I had a problem with the lerp() function. I was making zipline mechanics. When I put the lerp() line code in the zipline script for the sliding thing, the player will slide into the end point of the zipline. But the movements are instantaneous.

Here’s my code for Ziplines.

extends Node2D

@onready var playerchar: CharacterBody2D = get_node("/root/level/player")
@onready var startrope: Vector2 = $start.position
@onready var endrope: Vector2 = $end.position
@export var rope_color: Color
@export var rope_width: float
@export var rope_speed: int


func _ready():
	pass
	

func _draw():
	draw_line(startrope,endrope,rope_color,rope_width,true)
	$start/point.modulate = rope_color
	$end/point.modulate = rope_color

func start_zipline():
	playerchar.position = lerp(startrope, endrope,0.5)

func _on_start_body_entered(body):
	if playerchar == body:
		start_zipline()
	pass

And this is the one-line code of usage lerp().

playerchar.position = lerp(startrope, endrope,0.5)

Are there any ideas for solving the problems?

But if I put lerp() in the player script, the player moves well. But I can’t set the position of ropes in the player script since it will be overwritten with other inheritances of Node “Ropes”.

You code moves the player to the midpoint between startrope and endrope. For continuous movement you need to change the position once per frame.
For example:

var progress = 0.0
const SPEED = 0.5

func _ready():
    set_physics_process(false)

func start_zipline():
    playerchar.position = startrope
    set_physics_process(true)
    progress = 0.0

func end_zipline():
    playerchar.position = endrope
    set_physics_process(false)

func _physics_process(delta):
    progress += SPEED * delta
    if progress >= 1.0:
        end_zipline()
    else:
        playerchar.position = startrope.lerp(endrope, progress)

It may also be a good idea to disable player movement, while the zipline is active.

Depending your game design, using Tweens could be a valid solution as well.

2 Likes