![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Hanyuu |
Hi, im trying to make my character stands at a diagonal direction more precisely when releasing two keys. The code is working, but the problem is that its a bit hard to release the two keys at the exactly same time, the character always change to a horizontal or vertical diretion due to the last key released. I want to make it a bit more easy to keep facing the proper diagonal direction when he stop.
I’ll put my code here, im just starting with this engine and any suggestion to improve my code would be cool.
extends Node2D
var movedir = Vector2() # move direction
var speed = 100 # How fast the player will move (pixels/sec).
var lastdir = -1
func get_movedir():
var LEFT = Input.is_action_pressed(“left”)
var RIGHT = Input.is_action_pressed(“right”)
var UP = Input.is_action_pressed(“up”)
var DOWN = Input.is_action_pressed(“down”)
movedir.x = -int(LEFT) + int(RIGHT) # if pressing both directions this will return 0
movedir.y = -int(UP) + int(DOWN)
if movedir.length() > 1:
speed = 75
else:
speed = 100
func _process(delta):
get_movedir()
position += movedir * speed * delta
$Label.text = String(movedir.x) + " / " + String(movedir.y)
#sprite move 4 dir
if movedir.x == -1:
$AnimatedSprite.play("PlayerMoving")
$AnimatedSprite.rotation_degrees = -90
elif movedir.x == 1:
$AnimatedSprite.play("PlayerMoving")
$AnimatedSprite.rotation_degrees = 90
elif movedir.y == 1:
$AnimatedSprite.play("PlayerMoving")
$AnimatedSprite.rotation_degrees = 0
elif movedir.y == -1:
$AnimatedSprite.play("PlayerMoving")
$AnimatedSprite.rotation_degrees = 180
else:
$AnimatedSprite.play("PlayerIdle")
#sprite move 8 dir
if movedir.x == 1 and movedir.y == 1:
$AnimatedSprite.play("PlayerMoving")
$AnimatedSprite.rotation_degrees = 135
if movedir.x == 1 and movedir.y == -1:
$AnimatedSprite.play("PlayerMoving")
$AnimatedSprite.rotation_degrees = 45
if movedir.x == -1 and movedir.y == 1:
$AnimatedSprite.play("PlayerMoving")
$AnimatedSprite.rotation_degrees = -135
if movedir.x == -1 and movedir.y == -1:
$AnimatedSprite.play("PlayerMoving")
$AnimatedSprite.rotation_degrees = -45