Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An example of a script for KinematicBody2D that impelements basic player and enemy behavior.
extends KinematicBody2D
export var speed:int = 400
export var health:int = 10
func _physics_process(delta:float)->void:
process_inputs(delta)
pass
func process_inputs(delta)->void:
var velocity = Vector2(0, 0)
if Input.is_action_pressed("ui_right"):
velocity.x = 1
if Input.is_action_pressed("ui_left"):
velocity.x = -1
if Input.is_action_pressed("ui_down"):
velocity.y = 1
if Input.is_action_pressed("ui_up"):
velocity.y = -1
if Input.is_action_just_pressed("ui_fire"):
fire_projectile()
process_movement(delta, velocity)
pass
func process_movement(delta:float, velocity:Vector2)->void:
var size = get_viewport().size
var temp = velocity * (speed * delta)
print(temp)
move_and_slide(temp)
pass
func add_health(value:int)->void:
health+=value
func is_alive()->bool:
return health > 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment