Skip to content

Instantly share code, notes, and snippets.

@kenmorechalfant
Last active July 22, 2020 22:25
Show Gist options
  • Save kenmorechalfant/13fd81514d9e3734145a280a981bd3ca to your computer and use it in GitHub Desktop.
Save kenmorechalfant/13fd81514d9e3734145a280a981bd3ca to your computer and use it in GitHub Desktop.
Godot: Draw trail with Line2D
extends KinematicBody2D
var speed = 500
var velocity := Vector2.ZERO
var input_vector := Vector2.ZERO
var current_line: Line2D
func _process(delta):
update_move(delta)
update_line(delta)
func update_move(delta):
input_vector.x = -int(Input.is_action_pressed("ui_left")) + int(Input.is_action_pressed("ui_right"))
input_vector.y = -int(Input.is_action_pressed("ui_up")) + int(Input.is_action_pressed("ui_down"))
input_vector = input_vector.normalized()
velocity = lerp(velocity, input_vector * speed, 0.15)
position += (velocity * delta)
func update_line(delta):
var should_start_line = Input.is_action_just_pressed("ui_select")
var should_end_line = Input.is_action_just_released("ui_select")
if (should_start_line):
start_line()
elif (should_end_line):
current_line = null
if (is_instance_valid(current_line)):
current_line.add_point(position)
func start_line():
current_line = Line2D.new()
get_tree().get_root().add_child(current_line)
current_line.add_point(position)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment