Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | GodlyGladius |
So I tried to make my character jump (This is probably horrible code, this is my first time coding) But sometime it just hop a little when I pressed jump.
This is the script for my KinematicBody2D (GDScript)
extends KinematicBody2D
const GRAVITY = 2000
const WALK_SPEED = 750
const ACCELERATION = 50
const JUMP_SPEED = -1000
var velocity = Vector2()
func _physics_process(delta):
if !is_on_floor():
velocity.y += delta * GRAVITY
if Input.is_action_pressed("ui_left"):
velocity.x = velocity.x + -ACCELERATION
elif Input.is_action_pressed("ui_right"):
velocity.x = velocity.x + ACCELERATION
elif velocity.x != 0:
if velocity.x > 0:
velocity.x = velocity.x + -ACCELERATION
else:
velocity.x = velocity.x + ACCELERATION
else:
velocity.x = 0
if velocity.x > WALK_SPEED:
velocity.x = WALK_SPEED
elif velocity.x < -WALK_SPEED:
velocity.x = -WALK_SPEED
if is_on_ceiling():
velocity.y = -velocity.y
if Input.is_action_just_pressed("ui_up") and is_on_floor():
velocity.y = velocity.y + JUMP_SPEED
move_and_slide(velocity, Vector2(0, -1))