Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Francesco Cicala | |
Old Version | Published before Godot 3 was released. |
Hello guys, I started few days ago with GDScript and I love it. Now I’m facing this problem: I want my paddle to move in 8 directions (north, east, west, south and their combinations). As you can see I used variables to memorize the velocity components, and the new position is got by doing the normalized vector sum with these components.
Here is the behavior:
LEFT+RIGHT = paddle doesn’t move = UP + DOWN
LEFT+DOWN+RIGHT = paddle goes down (horizontals components reciprocally nullify)
RIGHT+DOWN+LEFT = paddle goes down (same)
DOWN+RIGHT+UP = paddle goes right
But here the unexpected behaviours:
LEFT+UP+RIGHT = paddle goes to the North-West, not only North, as if the RIGHT key wasn’t being pressed.
RIGHT+UP+LEFT: paddle goes to the North-East, as if the third key wasn’t being pressed.
DOWN+LEFT+UP: paddle goes to the South-East
I can’t explain why, I would say that the third key pressed isn’t registered but it’s false, it seems it isn’t only in some cases. Could you help me to figure out this problem?
#
extends KinematicBody2D
const SPEED = 150
var hor_speed = 0
var ver_speed = 0
var norm = 1
func _ready():
set_process_input(true)
set_process(true)
func _input(event):
if event.is_action_released("ui_down") or event.is_action_pressed("ui_up"):
ver_speed += -SPEED
if event.is_action_released("ui_up") or event.is_action_pressed("ui_down"):
ver_speed += SPEED
if event.is_action_released("ui_left") or event.is_action_pressed("ui_right"):
hor_speed += SPEED
if event.is_action_released("ui_right") or event.is_action_pressed("ui_left"):
hor_speed += -SPEED
func _process(delta):
norm = sqrt(hor_speed*hor_speed + ver_speed*ver_speed)
if (norm == 0):
norm = 1
else:
pass
set_pos(Vector2(get_pos().x + (hor_speed*delta)*abs(hor_speed)/norm, get_pos().y + (ver_speed*delta)*abs(ver_speed)/norm))