2
\$\begingroup\$

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

Player going right Player going up in another room

\$\endgroup\$
2
  • \$\begingroup\$ To be clear, if the player is holding "Right" when the transition in your diagram happens, which of these should happen? * Character stops until new input * Character moves right on screen (effectively taking a 90 degree turn after passing the portal) * Character continues to move forwards (up) even though the right key is being held (effectively retaining the old bindings relative to character orientation, rather than ones for the new screen orientation) [possibly only until the input is released] \$\endgroup\$
    – Basic
    Nov 13, 2023 at 22:03
  • \$\begingroup\$ The last one. The bindings are retained while the movement is rotated. Also, @thirteen 's solution worked, but I'll also accept anything more efficient. \$\endgroup\$
    – value1
    Nov 14, 2023 at 1:41

1 Answer 1

2
\$\begingroup\$

Not tested, but you could try looking at which keys the player is pressing when they're warping and setting the velocity x/y values to 0 when they're released depending on how much the movement is rotated.

For example, if the player is holding the "right" key when going into the right portal and it rotates it to go up, you could link releasing the "right" key with resetting the y velocity.

Also, if you're going diagonally, you could record two values, like "up" and "right", and then link them to resetting the x and y velocities. However, if you rotate 180 degrees (PI radians), then the axes will remain the same.

Hope this helps!

\$\endgroup\$
1
  • \$\begingroup\$ Seems like something worth trying! I'll test this out later. Thanks! \$\endgroup\$
    – value1
    Nov 13, 2023 at 14:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .