Hi,
I'm a little unsure as what I need to change, perhaps the whole code so far for movement.
I'm trying to make a classic 2D platform player movement similar to Super Mario. When the player jumps they should be able to flip direction (just the sprite), but not move_and_slide in the opposite direction. So once the direction for the movement is set, you shouldn't be able to reverse - you are committed to that direction only.
The way I have it so far is kinda neat and allows you to change your mind mid air, but it's not the right feel for what I want.
Any Ideas what to change?
# Simple Platform Movement Script
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 - Allows the jump to end when key jump button released
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 - adding it to bear_velocity slows the gravity down issue
bear_velocity = move_and_slide(bear_velocity, FLOOR)
Thanks!