Skip to content

Instantly share code, notes, and snippets.

@huntlyc
Last active July 16, 2024 03:08
Show Gist options
  • Save huntlyc/37a310a23d0995c4ad030bfc933d33c5 to your computer and use it in GitHub Desktop.
Save huntlyc/37a310a23d0995c4ad030bfc933d33c5 to your computer and use it in GitHub Desktop.
Godot Simple 2d Movement
extends KinematicBody2D
var speed = Vector2(500, 2500)
var gravity = 8000
var friction = 0.1
var acceleration = 0.2
var velocity = Vector2.ZERO
func _physics_process(delta: float) -> void:
var dir = get_direction()
var is_jump_interupted = Input.is_action_just_released("jump") and velocity.y < 0.0
velocity = calculate_velocity(velocity, dir, is_jump_interupted, speed, delta)
velocity = move_and_slide(velocity, Vector2.UP)
func get_direction():
return Vector2(
Input.get_action_strength("right") - Input.get_action_strength("left"),
-1.0 if Input.is_action_just_pressed("jump") and is_on_floor() else 0.0
)
func calculate_velocity(curVelocity, dir, is_jump_interupted, speed, delta):
var out = Vector2.ZERO
if dir.x != 0:
out.x = lerp(curVelocity.x, dir.x * speed.x, acceleration)
else:
out.x = lerp(curVelocity.x, 0, friction)
if(dir.y == -1.0): # Jumping
out.y = dir.y * speed.y
else: # Falling
out.y = curVelocity.y + (gravity * delta)
if(is_jump_interupted): # jump key release
out.y = 0.0 + (gravity * delta)
return out
@tech-with-the-unkownguy

thx my guy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment