Skip to content

Instantly share code, notes, and snippets.

@NovaAndrom3da
Last active April 18, 2022 15:53
Show Gist options
  • Save NovaAndrom3da/2787292680ce60b3831a554bdda7880e to your computer and use it in GitHub Desktop.
Save NovaAndrom3da/2787292680ce60b3831a554bdda7880e to your computer and use it in GitHub Desktop.
Very Bad jumping physics in Godot
extends KinematicBody2D
var screen_size
var velocity = Vector2.ZERO
func _ready():
screen_size = get_viewport_rect().size
$CollisionShape2D.position.bounce = 0
func canjump(): # A temporary function. Will return more useful data later
return true
export var fall_gravity_scale := 150.0
export var low_jump_gravity_scale := 100.0
export var jump_power := 10.0
var jump_released = false
var earth_gravity = 9.807 # m/s^2
export var gravity_scale := 10.0
export var speed = 400
func _physics_process(delta):
velocity += Vector2.DOWN * earth_gravity * gravity_scale * delta
if velocity.y > 0:
velocity += Vector2.DOWN * earth_gravity * fall_gravity_scale * delta
if velocity.y < 0 && jump_released:
velocity += Vector2.DOWN * earth_gravity * low_jump_gravity_scale * delta
if canjump():
if Input.is_action_pressed("jump"):
velocity = Vector2.UP * jump_power
jump_released = false
if velocity.length() > 0:
velocity = velocity.normalized() * speed
position += velocity * delta
# position.y = clamp(position.y, 16, screen_size.y - 16)
velocity = move_and_slide(velocity, Vector2.UP)
func _process(delta):
var dir = 0
if Input.is_action_pressed("reload"):
get_tree().change_scene("res://assets/scenes/load.tscn")
if Input.is_action_pressed("move_right"):
velocity.x += 10
if Input.is_action_pressed("move_left"):
velocity.x -= 10
if Input.is_action_just_pressed("LMB"):
var mouse_pos = get_global_mouse_position()
position.x = mouse_pos.x
position.y = mouse_pos.y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment