Hi,
I'm having difficulty changing the typical game mechanic I've seen in the majority of tutorials for platform game character movement with KinematicBody2D in Godot.
I'm trying to create a movement typical of 80's platform games, such as Super Mario. Where the player jumps and cannot reverse the direction of movement. Flip the sprite, yes, but not go against physics!
The desired outcome is to force the player to only jump in the direction they chose, so they are committed to it.
It's absolutely baffling how to implement it. I've attempted functions to split the controls up, but it just became too complicated as it then affected the jumping too. Plus I was creating the same code twice, which looked amateurish at best - it still didn't work though ;)
Anyway, here's my basic code before I attempted to alter it:
extends KinematicBody2D
var bear_velocity = Vector2()
var on_ground = false
const SPEED = 60
const GRAVITY = 10
const FLOOR = Vector2(0,-1)
const JUMP_POWER = -242
func _physics_process(delta):
# Walking Left, Right or Idle
if Input.is_action_pressed("ui_right"):
$Sprite.flip_h = false
bear_velocity.x = SPEED
$Sprite.play("Walk")
elif Input.is_action_pressed("ui_left"):
$Sprite.flip_h = true
$Sprite.play("Walk")
bear_velocity.x = -SPEED
else:
bear_velocity.x = 0
if on_ground == true:
$Sprite.play("Idle")
# Jumping
if is_on_floor():
on_ground = true
else:
on_ground = false
if bear_velocity.y < 0:
$Sprite.play("Jump")
else:
$Sprite.play("Fall")
if Input.is_action_pressed("ui_select"):
if on_ground == true:
bear_velocity.y = JUMP_POWER
on_ground = false
# Variable Height Jump
if Input.is_action_just_released("ui_up") && bear_velocity.y < -50:
bear_velocity.y = -50
# Add Gravity
bear_velocity.y += GRAVITY
# Add Movement from Vector2
bear_velocity = move_and_slide(bear_velocity, FLOOR)