Hi, I am trying to make a clone of a classic top-down snake game. So far, I have a CharacterBody2D that moves and flips when I move left or right. However, I want the sprite to move to face up and down when the player moves it in those directions. How would I go about that? Would I use an animation player? I am somewhat new to programming, and I’m trying to do more than just follow a tutorial mindlessly. I will post my Player script below. Any advice would be appreciated, thanks!
extends CharacterBody2D
# speed in pixels/sec
var speed = 500
func _physics_process(_delta):
# setup direction of movement
var direction = Input.get_vector("left", "right", "up", "down")
# stop diagonal movement by listening for input then setting axis to zero
if Input.is_action_pressed("right") || Input.is_action_pressed("left"):
direction.y = 0
elif Input.is_action_pressed("up") || Input.is_action_pressed("down"):
direction.x = 0
else:
direction = Vector2.ZERO
#flip the sprite based on left/right direction
if Input.is_action_pressed("right"):
$Sprite2D.flip_h = true
else: if Input.is_action_pressed("left"):
$Sprite2D.flip_h = false
#normalize the directional movement
direction = direction.normalized()
# setup the actual movement
velocity = (direction * speed)
move_and_slide()
Are you saying I’d use the same code but change it to flip_v? I think I can handle that on my own, I didn’t realize it would work like that too. Now that I’m thinking about it, I’m pretty sure I’ve just jumped the gun in asking on here and I could have figured it out on my own. Thank you for the push in the correct direction though!