I have a 2d game (Godot 4) with different rooms with portals to go between them, and some portals redirect the direction of the player while still holding the same key. I have that working so far, but when I release the key, it keeps moving in the redirected direction. How can I make sure the player stops when it's supposed to?
Movement:
if Input.is_action_just_pressed("left"):
velocity.x = -1
$Sprite2D.flip_h = false
elif Input.is_action_just_pressed("right"):
velocity.x = 1
$Sprite2D.flip_h = true
if Input.is_action_just_released("left"):
velocity.x = max(velocity.x,0)
if Input.is_action_just_released("right"):
velocity.x = min(velocity.x,0)
if Input.is_action_just_pressed("up"):
velocity.y = -1
elif Input.is_action_just_pressed("down"):
velocity.y = 1
if Input.is_action_just_released("up"):
velocity.y = max(velocity.y,0)
if Input.is_action_just_released("down"):
velocity.y = min(velocity.y,0)
Warp between rooms:
func warp(real_dir,dir): # ULDR
var odd = dir % 2 == 1
var vpos = position
var off = -2*MARGIN
var offw = SIZE - 2*MARGIN
var diff = player.position - (position + Vector2(SIZE/2,SIZE/2))
player.position = diff.rotated((real_dir - dir) * PI/2) + position + Vector2(SIZE/2,SIZE/2)
if dir >= 2:
off = SIZE + 2*MARGIN
offw = 2*MARGIN
if odd:
vpos = Vector2(vpos.y,vpos.x)
player.position = Vector2(player.position.y,player.position.x)
if warps[dir] == "":
player.position.y = vpos.y + off
else:
var level_pos = locate_level(warps[dir])
if odd:
level_pos = Vector2(level_pos.y,level_pos.x)
player.position.x += level_pos.x - vpos.x
player.position.y = level_pos.y + offw
if odd:
player.position = Vector2(player.position.y,player.position.x)
player.velocity = player.velocity.rotated((real_dir - dir) * PI/2)
player_inside = not warps[dir] == name