|
|
|
|
Reply From: |
SIsilicon |
Have you looked at the 2D platform demo? The way you are implementing character movement is not very good.
The first problem looks like it happened due to the fact that once you press up, you keep applying the jump height velocity. Causing it to pretty much “fall up” until it hits a floor.
The second problem is so because you’re character is not adding velocity. It is setting it.
Both of these problems, plus others that may arise along the way can be solved by storing a velocity variable. var linear_velocity = Vector2()
. And use this with move_and_slide
instead. If you want to apply forces to your character, like jump for instance, you add that force to linear_velocity
and it gets taking care of by move_and_slide
.
So here’s how it should work.
#the following code is inside physics_process. Note that linear_velocity was defined outside of this function.
direction = MoveDirection.NONE
#First we add gravity to the velocity.
linear_velocity.y += GRAVITY
#Apply horizontal friction. So that the character doesn't slide forever.
linear_velocity.x *= 0.0
#We then check if the player wants to move left or right.
#And if so, add a horizontal force to linear_velocity.
if Input.is_action_pressed("ui_left"):
linear_velocity.x -= MOVE_SPEED
direction = MoveDirection.LEFT
if Input.is_action_pressed("ui_right"):
linear_velocity.x += MOVE_SPEED
direction = MoveDirection.RIGHT
#Check if you can and should jump.
if is_on_floor() and Input.is_action_just_pressed("ui_up"):
#And if so, add a vertical force going up.
linear_velocity.y -= JUMP_SPEED
if not is_on_floor():
direction = MoveDirection.UP
#The most important step. Setting linear_velocity to the result of move_and_slide.
linear_velocity = move_and_slide(linear_velocity, Vector2(0, -1))
#Change animation according to direction.
match direction:
MoveDirection.NONE: _toon_idle()
MoveDirection.UP: _toon_jump()
MoveDirection.LEFT: _toon_left()
MoveDirection.RIGHT: _toon_right()
Please read the comments if you want to understand what’s going on.
By the by, I written all that without testing it, but in theory it should work. If there is a problem, let me know.
SIsilicon | 2018-09-21 17:43
Hey thanks man Im getting very close, just one more little question, after my changes I ended up with this:
extends KinematicBody2D
var direction = null;
const MOVE_SPEED = 1000;
const JUMP_SPEED = 1000;
const GRAVITY = 150;
enum MoveDirection { UP, LEFT, RIGHT, NONE }
var linear_velocity = Vector2();
func _ready():
set_process_input(true)
pass
func _physics_process(delta):
direction = MoveDirection.NONE
linear_velocity.y += GRAVITY
linear_velocity.x *= 0.0
if Input.is_action_just_pressed("ui_left"):
linear_velocity.x -= MOVE_SPEED
direction = MoveDirection.LEFT
if Input.is_action_just_pressed("ui_right"):
linear_velocity.x += MOVE_SPEED
direction = MoveDirection.RIGHT
if is_on_floor() and Input.is_action_just_pressed("ui_up"):
linear_velocity.y -= JUMP_SPEED
if not is_on_floor():
direction = MoveDirection.UP
linear_velocity = move_and_slide(linear_velocity, Vector2(0, -1))
match direction:
MoveDirection.NONE: _toon_idle()
MoveDirection.UP: _toon_jump()
MoveDirection.LEFT: _toon_left()
MoveDirection.RIGHT: _toon_right()
func _toon_idle():
$toon.play("idle")
func _toon_jump():
$toon.play("jump")
func _toon_right():
$toon.flip_h = false
$toon.play("run")
func _toon_left():
$toon.flip_h = true
$toon.play("run")
Jumping definitely works a lot better, but moving has gotten funny, I have to be pressing either left or right repeatedly to be able to move a little bit. where did I messed up?
Change is_action_just_pressed
to is_action_pressed
Yes that’s right. In my example code I used is_action_just_pressed
for the jump’s if condition. The difference between the two functions is that when you trigger the specified action, is_action_just_pressed
becomes true for only one frame, while is_action_pressed
stays true until you release the action’s trigger.
So do what Oen44 said, but only for “ui_ left” and “ui_ right”.
SIsilicon | 2018-09-21 21:17