I need help to figure out how to make my character slide. When the player moves left and switches to right I want a smooth transition so I made a slide and turn animation but I cant put it in my code. I tried with vector2 comparison but because its a float its always bumping. I think I need to compare input from frame to frame but dont know how :S.
Here’s my code so far:
extends CharacterBody2D
@export var speed : float = 3000
@export var jump_velocity : float = -800.0
@export var double_jump_velocity : float = -800
const acceleration = 200
var is_sliding : bool = false
var is_running : bool = false
var has_double_jumped : bool = false
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var animation_locked : bool = false
@onready var anim = get_node("AnimatedSprite2D")
func _ready():
get_node("AnimatedSprite2D").play("Idle")
func _physics_process(delta):
var move_direction = Input.get_axis("left", "right")
if not is_on_floor():
velocity.y += gravity * delta
speed = 3200
else:
has_double_jumped = false
speed = 3000
if Input.is_action_just_pressed("jump"):
if is_on_floor():
velocity.y = jump_velocity
elif not has_double_jumped:
velocity.y = double_jump_velocity
has_double_jumped = true
if velocity.x != 0:
get_node("AnimatedSprite2D").flip_h = velocity.x > 0
if move_direction:
velocity.x = move_direction * speed
is_running = true
if is_running:
anim.play("Run")
if move_direction < 0 and Input.is_action_just_pressed("right"):
anim.play("Turn_Slide")
else:
anim.play("Idle")
velocity.x = move_toward(velocity.x, 0, speed)
move_and_slide()
I have a possible solution for you. Looking at the code, I realised that you’re making things more complicated than they need to be. You don’t need boolean variables to change states.
Instead, create enum current_state {idle, sliding, running… and so on}
Now a variant of how to do the check, in fact you have const acceleration, but you do not use it and I think further it will not be useful.
As you have it: if you go left and then suddenly pressed right, you immediately changed the direction of movement.
How you can fix it: if you are going left and then suddenly pressed right, you should wait for a stop (like move_toward) and then only turn the character. And now you will have an equal-accelerated change of direction and at the moment when you change direction you should play the animation of sliding
Not sure I get everything you are saying. But I tried it and all it does now is once I press left or right the slide animation loops and the character doesnt move
Take the loop off the animation.
There is also a 0/0 exception, i.e. in the line vel.x/abs(vel.x) (fixed) we were getting the current direction of movement, but initially the character is standing and so nothing worked.
if direction:
if vel.x != 0:
if vel.x/abs(vel.x) != direction:
move_toward(vel.x, 0, accel)
if anim.name != "slide"
anim.play("slide")
else:
anim.play("run")
vel.x = speed * dir
else:
anim.play("run")
vel.x = speed * dir
It’s not a direct solution, it’s just a scheme. Something like this. It’s hard for me to explain my train of thought
Hey just tried that and feels like this is almost it! The Run cycle doesnt start once the slide animation is finished, it only stay on the last frame and also once the left or right key is pressed I can’t change the input and the character just keep getting in the direction of the first input even when nothing is pressed. Thanks a lot once again I’m an artist that started to code 2 weeks ago XD.