Lerping/easing movement on a navigation path in a 3D point-and-click game?

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

Hi Guys. I’m testing a point-and-click movement mechanics in 3D, which is working fine. I have a move_to function which gets the path between the character’s current position and target. The target is assigned in the main script (basically casting a ray from the mouse and capturing the position where collides with the ground). I’m using the move_and_slide method to move the character along the path towards its target position.
Question: how do I ease-in the movement so that the character doesn’t abruptly stop when it reaches the target position??

Here is the character script:

var path = []
var path_index = 0
var move_speed = 12
onready var nav = navigation node

func move_to(target_position):
	path = nav.get_simple_path(global_transform.origin, target_position)
	path_ind = 0

func _physics_process(delta):
	if path_index < path.size():
		var move_vec = (path[path_index] - global_transform.origin)
		if move_vec.length() < 0.1:
			path_index += 1
		else:
			move_and_slide(move_vec.normalized() * move_speed, Vector3(0, 1, 0))
:bust_in_silhouette: Reply From: the.Alien

why not simply reducing the move-speed when you’re approaching the final target position? You already know the move-vec vector, so from its length you know how far you are from the final position…

I was thinking about this actually. Eg, if distance between the character and target position is less than something, move_speed -= 1. I’ll try this out.

Macryc | 2020-03-24 18:04

:bust_in_silhouette: Reply From: Calinou

You can use the Tween node for this purpose. See this tutorial for a practical example.

I’ve already tried. Tween works beautifully here but it interpolates between the start and end position only, meaning it ignores the navigation mesh and the character is happily passing through walls etc.

Macryc | 2020-03-24 18:02