Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Gonz4 L |
I want that if the player goes +2 or -2 taking into account the x position of the player before pressing left or right, the fall animation change and keeps changed (even if turning to the other side) until the player lands.
Here’s my code:
extends KinematicBody2D
const UP = Vector2(0, -1)
const GRAVITY = 20
const MAXFALLSPEED = 200
const MAXSPEED = 80
const JUMPFORCE = 300
const ACCEL = 10
var motion = Vector2()
func _physics_process(delta):
motion.y += GRAVITY
if motion.y > MAXFALLSPEED:
motion.y = MAXFALLSPEED
motion.x = clamp(motion.x, -MAXSPEED, MAXSPEED)
if Input.is_action_pressed("ui_right"):
motion.x += ACCEL
$Spr_P.set_flip_h(false)
if is_on_floor():
$AnimationPlayer.play("Walk")
if !is_on_floor():
if motion.y < 0:
$AnimationPlayer.play("Jump")
elif motion.y > 0:
if position.x + 1:
$AnimationPlayer.play("Fall Forward")
else:
$AnimationPlayer.play("Fall")
elif Input.is_action_pressed("ui_left"):
motion.x -= ACCEL
$Spr_P.set_flip_h(true)
if is_on_floor():
$AnimationPlayer.play("Walk")
if !is_on_floor():
if motion.y < 0:
$AnimationPlayer.play("Jump")
elif motion.y > 0:
if position.x - 1:
$AnimationPlayer.play("Fall Forward")
else:
$AnimationPlayer.play("Fall")
else:
motion.x = lerp(motion.x, 0, 0.2)
if is_on_floor():
$AnimationPlayer.play("Idle")
if !is_on_floor():
if motion.y < 0:
$AnimationPlayer.play("Jump")
elif motion.y > 0:
$AnimationPlayer.play("Fall")
if is_on_floor():
if Input.is_action_just_pressed("ui_up"):
motion.y = -JUMPFORCE
motion = move_and_slide(motion, UP)
I’m not sure if I understand properly. What do you mean by “if the player goes +2 or -2”? Are you talking about the x-position of the player? In pixels? Or tiles?
exuin | 2020-10-01 00:53
Pixels (i tried if position.x + 2/- 2 (well, i still have to see how much) but it doesn’t keeps the change).
Gonz4 L | 2020-10-01 01:25
I’m assuming that you’re talking about the “Fall Forward” animation in the image. There are some issues with your if statements.
position.x + 1
is an integer, not a boolean. When converting ints to bools, anything that’s not 0 is true, so the “Fall” animation will only play if the player is at 1 or -1.
But what do you mean by “if the player goes +2 or -2 taking into account the x position of the player before pressing left or right” still? Are you saying you want to play a new animation and not stop it if the player character moves two pixels after pressing the left or right button? And do you want to flip the animation or not?
exuin | 2020-10-01 01:58
My animation of the middle (“Fall”) it’s used if the player jumps without using right or left (x + 0) or before reaching +2/-2 pixels (if the x position before jumping was 960, then when 962 or 958 is reached change the animation to “Fall Forward” (if motion.y > 0 too) and keep it (without mattering if the player releases right or left or turns to the other side) until the character lands.
And yes, the animations have to be flipped.
Edit: Solved.
Gonz4 L | 2020-10-01 14:24