godot 3d movement tutorial code example
Example 1: godot platformer movement
extends KinematicBody2D
export var speed = 500
export var gravity = 32
export var jumpforce = 800
var motion = Vector2.ZERO
func _physics_process(delta):
if Input.is_action_pressed("ui_right"):
motion.x = speed
elif Input.is_action_pressed("ui_left"):
motion.x = -speed
else:
motion.x = lerp(motion.x, 0, 0.25)
if is_on_floor():
if Input.is_action_pressed("ui_up"):
JumpSound.play()
motion.y = -jumpforce
motion.y += gravity + delta
motion = move_and_slide(motion, Vector2.UP)
Example 2: movement in godot
extends KinematicBody
var speed = 200
var motion = Vector2()
func _physics_process(delta):
if Input. is_action_pressed("ui_right"):
motion.x += speed
elif Input. is_action_pressed("ui_left"):
motion.x -= speed
elif Input. is_action_pressed("ui_up"):
motion.z -= speed
elif Input. is_action_pressed("ui_down"):
motion.z += speed
else:
motion.x = 0
move_and_collide(motion)