0

I tried using the lerp() function to do linear interpolation in GODOT, however; the object in my project just abruptly stops, like it never slows down.

extends KinematicBody

var SPEED = 200
var direction = Vector3()

func _ready():
    pass

func _physics_process(_delta):
    direction = Vector3(0,0,0)
    if Input.is_action_pressed("ui_left"):
        direction.x -= 1
        lerp(direction.x, 0, 1)
    if Input.is_action_pressed("ui_right"):
        direction.x += 1
        lerp(direction.x, 0, 1)
    if Input.is_action_pressed("ui_up"):
        direction.z -= 1
        lerp(direction.z, 0, 1)
    if Input.is_action_pressed("ui_down"):
        direction.z += 1
        lerp(direction.z, 0, 1)
        
    direction = direction.normalized()
    direction = direction * SPEED * _delta
    move_and_slide(direction, Vector3(0,1,0))

  • You can achieve this by adding acceleration vector: https://godotengine.org/qa/44522/smooth-movement-without-gravity – Yuraj Mar 29 '21 at 10:28

1 Answers1

0

With setting the last argument of the lerp function to „1“, you set the weight of the second argument to 100%. So it immediately jumps to this position. To get the value smoothly slowing down, you have to decrease this value. (Possible values are from 0 to 1.0)

Some examples:

lerp(0, 4, 0.75) # Returns 3.0
lerp(Vector2(1, 5), Vector2(3, 2), 0.5) # Returns Vector2(2, 3.5)
Spyrex
  • 103
  • 8