0 votes

In Garbaj's tutorial on making a better fps controller he uses this code:

extends KinematicBody

var speed = 15
var h_acceleration = 8
var air_acceleration = 1
var normal_acceleration = 6
var gravity = 30
var jump = 10

var full_contact = false

var mouse_sensetivity = 0.3

var direction = Vector3()
var h_velocity = Vector3()
var movement = Vector3()
var gravity_vec = Vector3()

onready var head = $head
onready var ground_check = $ground_check

func _ready():
    Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _input(event):

    if event is InputEventMouseMotion:
        rotate_y(deg2rad(-event.relative.x * mouse_sensetivity))
        head.rotate_x(deg2rad(event.relative.y * mouse_sensetivity))

        head.rotation.x = clamp(head.rotation.x,deg2rad(-89), deg2rad(89))

func _physics_process(delta):

    direction = Vector3()

    if ground_check.is_colliding():
        full_contact = true
    else:
        full_contact = false

    if not is_on_floor():
        gravity_vec += Vector3.DOWN * gravity * delta
        h_acceleration = air_acceleration
    elif is_on_floor() and full_contact:
        gravity_vec = -get_floor_normal() * gravity
        h_acceleration = normal_acceleration
    else:
        gravity_vec = get_floor_normal()
        h_acceleration = normal_acceleration


    if Input.is_action_just_pressed("jump") and (is_on_floor() or ground_check.is_colliding()):
        gravity_vec = Vector3.UP * jump

    if Input.is_action_pressed("move_forward"):
        direction += transform.basis.z
    elif Input.is_action_pressed("move_backward"):
        direction -= transform.basis.z
    if Input.is_action_pressed("move_left"):
        direction += transform.basis.x
    elif Input.is_action_pressed("move_right"):
        direction -= transform.basis.x

    direction = direction.normalized()
    h_velocity = h_velocity.linear_interpolate(direction * speed, h_acceleration * delta)
    movement.z = h_velocity.z + gravity_vec.z
    movement.x = h_velocity.x + gravity_vec.x
    movement.y = gravity_vec.y

    move_and_slide(movement,Vector3.UP)

I understand it pretty well, but I'm having trouble with this section:

h_velocity = h_velocity.linear_interpolate(direction * speed, h_acceleration * delta)
        movement.z = h_velocity.z + gravity_vec.z
        movement.x = h_velocity.x + gravity_vec.x
        movement.y = gravity_vec.y

My original thought was thath_velocity.linear_interpolate() is the same as using a lerp function,but that doesn't make sense. If that's true, I think what his function would be doing is taking the movement of the character and lerping it to h_acceleration, which is 8. That would make zero sense. What is this code actually doing? Thanks.

in Engine by (385 points)

so far all I've found is this link.

1 Answer

+1 vote
Best answer

The difference is lerp interpolates between two scalers while linear_interpolate interpolates between two vectors, for both Vector2 and Vector3.

by (1,097 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.