Skip to content

Instantly share code, notes, and snippets.

@henriiquecampos
Last active June 15, 2022 14:09
Show Gist options
  • Save henriiquecampos/b119ce30ecba46e46fd32d00719a098d to your computer and use it in GitHub Desktop.
Save henriiquecampos/b119ce30ecba46e46fd32d00719a098d to your computer and use it in GitHub Desktop.
An example of simple platform character movement in Godot Engine
extends KinematicBody2D
const FLOOR_NORMAL = Vector2.UP
export(float) var speed = 500.0
export(float) var gravity = 2000.0
var direction = Vector2.ZERO
var velocity = Vector2.ZERO
onready var sprite = $Sprite
func _physics_process(delta):
velocity.y += gravity * delta
velocity = move_and_slide(velocity, FLOOR_NORMAL)
func _unhandled_input(event):
if event.is_action("left") or event.is_action("right"):
update_direction()
func update_direction():
direction.x = Input.get_action_strength("right") - Input.get_action_strength("left")
velocity.x = direction.x * speed
if not velocity.x == 0:
sprite.flip_h = velocity.x < 0
extends KinematicBody2D
const FLOOR_NORMAL = Vector2.UP
const SNAP_DIRECTION = Vector2.DOWN
const SNAP_LENGTH = 32
export(float) var speed = 500.0
export(float) var gravity = 2000.0
var direction = Vector2.ZERO
var velocity = Vector2.ZERO
var snap_vector = SNAP_DIRECTION * SNAP_LENGTH
onready var sprite = $Sprite
func _physics_process(delta):
velocity.y += gravity * delta
velocity.y = move_and_slide_with_snap(velocity, snap_vector, FLOOR_NORMAL, true).y
func _unhandled_input(event):
if event.is_action("left") or event.is_action("right"):
update_direction()
func update_direction():
direction.x = Input.get_action_strength("right") - Input.get_action_strength("left")
velocity.x = direction.x * speed
if not velocity.x == 0:
sprite.flip_h = velocity.x < 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment