Skip to content

Instantly share code, notes, and snippets.

@turnipsoup
Created June 10, 2021 21:45
Show Gist options
  • Save turnipsoup/2ac8721ab199f91cb711da60ea98e6f7 to your computer and use it in GitHub Desktop.
Save turnipsoup/2ac8721ab199f91cb711da60ea98e6f7 to your computer and use it in GitHub Desktop.
Example Godot-Script file for player movement for me to use for reference.
extends KinematicBody2D
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
const UP = Vector2(0,-1)
const GRAVITY = 20
const MAXFALLSPEED = 200
const MAXSPEED = 80
const JUMPFORCE = 300
const ACCEL = 10
var facing_right = true
var motion = Vector2()
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass
func _physics_process(delta):
motion.y += GRAVITY
if motion.y > MAXFALLSPEED:
motion.y = MAXFALLSPEED
if facing_right:
$Sprite.scale.x = 1
else:
$Sprite.scale.x = -1
motion.x = clamp(motion.x, -MAXSPEED, MAXSPEED)
if Input.is_action_pressed("right"):
motion.x += ACCEL
facing_right = true
$AnimationPlayer.play("Running")
elif Input.is_action_pressed("left"):
motion.x += -ACCEL
facing_right = false
$AnimationPlayer.play("Running")
else:
motion.x = lerp(motion.x,0,0.2)
$AnimationPlayer.stop()
if is_on_floor():
if Input.is_action_just_pressed("jump"):
motion.y = -JUMPFORCE
motion = move_and_slide(motion, UP)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment