Good evening, I was recently working on my character, and I was unsure about the best way to make it work, the best way to implement functional movement. Currently, my code is like this:
ᅠ
ᅠ
Looking at it, I already feel like it’s waaaaay better than the first version I made.
ᅠ
extends CharacterBody2D
const speed = 50
var current_dir = "none"
var accept_input:bool = true
func _physics_process(delta):
if (accept_input):
player_movement(delta)
move_and_slide()
func player_movement(_delta):
if Input.is_action_pressed("ui_right"):
current_dir = "right"
play_anim(1)
velocity.x = speed
velocity.y = 0
elif Input.is_action_pressed("ui_left"):
current_dir = "left"
play_anim(1)
velocity.x = -speed
velocity.y = 0
elif Input.is_action_pressed("ui_down"):
current_dir = "down"
play_anim(1)
velocity.y = speed
velocity.x = 0
elif Input.is_action_pressed("ui_up"):
current_dir = "up"
play_anim(1)
velocity.y = -speed
velocity.x = 0
else:
play_anim(0)
velocity.x = 0
velocity.y = 0
move_and_slide()
func play_anim(movement):
var dir = current_dir
var anim = $AnimatedSprite2D
if dir == "right":
anim.flip_h = true
if movement == 1:
anim.play("side_walk")
elif movement == 0:
anim.play("idle_back")
if dir == "left":
anim.flip_h = false
if movement == 1:
anim.play("side_walk")
elif movement == 0:
anim.play("idle_front")
if dir == "up":
anim.flip_h = true
if movement == 1:
anim.play("back_walk")
elif movement == 0:
anim.play("idle_back")
if dir == "down":
anim.flip_h = true
if movement == 1:
anim.play("front_walk")
elif movement == 0:
anim.play("idle_front")
ᅠ
The Yanderedev drama taught me a lot about not using if/else statements for absolutely everything (lol). Well, I don’t need super-code, my character just needs the ability to ‘interact’ with NPCs and the environment (I’m using the ‘Dialogue Manager’ plugin, made by Nathan Hoad) and - obviously - use animations for when it’s standing still or moving in different directions. What’s the best way to do this properly?
You can either use if / else statements (frankly I wouldn’t worry too much about overusing them) to check what direction the player is going in, or you can input the vector into an AnimationTree which will automatically pick the correct animation for the input vector.
You should have a separate de-acceleration constant instead of using speed. Right now, it moves towards zero by 100 per frame, which is too fast. You need to multiple it by delta. Also, it’s recommended that you make your own actions instead of using the built-in UI actions since those were specifically made for UI.
Oh, the reason it’s so fast is that I’m still in the testing phase. I don’t plan on making any speed changes, like running, etc. The idea is to make it look like a NES/SNES game. Do I really need acceleration instead of just a constant speed?
And, how to properly make my own actions in the right way?
It’s your game, if you want acceleration you can have acceleration. If you don’t want it that’s fine too.
To make your own actions, simply open project settings, go to the input map, type in a name, press the + sign, and then press the key you want to assign to that action.
Thanks for the clarification! I had mistaken ‘actions’ for something else. AnimationTree seems much more functional, thanks for the tip.
Regarding acceleration, visually speaking, do you think it’s a good idea to have at least a minimum change? As they say here in Brazil, ‘the devil is in the details’.
Oh, in that case it doesn’t sound like a good idea.
But after some testing, it seems like the character moves faster diagonally than in other x & y directions… Is this just a mistake on my part? And if not, is it possible to fix it?