GODOT - 3d Movement
1 min readMay 2, 2020
# Example 1:
extends KinematicBodyvar moveSpeed : float = 5.0
var velocity = Vector3()func _physics_process (delta):
velocity.x = 0
velocity.z = 0 var input = Vector3()
if Input.is_action_pressed(“move_forwards”):
input.z += 1
if Input.is_action_pressed(“move_backwards”):
input.z -= 1
if Input.is_action_pressed(“move_left”):
input.x += 1
if Input.is_action_pressed(“move_right”):
input.x -= 1
# normalize the input vector to prevent increased diagonal speed
input = input.normalized()
# get the relative direction
var dir = (transform.basis.z * input.z + transform.basis.x * input.x)
# apply the direction to our velocity
velocity.x = dir.x * moveSpeed
velocity.z = dir.z * moveSpeed
# move along the current velocity
velocity = move_and_slide(velocity, Vector3.UP)
# Example 2:
extends KinematicBodyvar speed = 4
var gravity = Vector3.DOWN * 12
var jump_speed = 6
var velocity = Vector3()
var spin = 0.1
var jump = falsefunc _physics_process(delta: float) -> void:
velocity += gravity * delta
get_input()
velocity = move_and_slide(velocity,Vector3.UP)func get_input():
var vy = velocity.y
velocity = Vector3() if Input.is_action_pressed(“move_forward”):
velocity += -transform.basis.z * speed
if Input.is_action_pressed(“move_back”):
velocity += transform.basis.z * speed
if Input.is_action_pressed(“strafe_left”):
velocity += -transform.basis.x * speed
if Input.is_action_pressed(“strage_right”):
velocity += transform.basis.x * speed jump = false
if Input.is_action_just_pressed(“jump”):
jump = true
velocity.y = vy