PLATFORMER - PLAYER

 avatar
Kandif
plain_text
a year ago
763 B
12
Indexable
Never
extends KinematicBody2D

var speed = 150
var jump = 400
var gravity = 800
var velocity = Vector2.ZERO

var jump_anim = false


func _process(delta):
	velocity.x = 0
	if Input.is_key_pressed(KEY_A):
		$AnimatedSprite.flip_h = true
		velocity.x = -speed
	elif Input.is_key_pressed(KEY_D):
		$AnimatedSprite.flip_h = false
		velocity.x = speed
		
	if !is_on_floor():
		$AnimatedSprite.animation = "jump"
		if velocity.y < 0:
			$AnimatedSprite.frame = 0
		elif velocity.y > 0:
			$AnimatedSprite.frame = 1
	elif velocity.x!=0:
		$AnimatedSprite.animation = "run"
	else:
		$AnimatedSprite.animation = "idle"	
	
	if Input.is_key_pressed(KEY_W) && is_on_floor():
		velocity.y = -jump 
		
	velocity.y += gravity * delta
	
	velocity = move_and_slide(velocity,Vector2.UP)