Discord
Login
Community
DARK THEME

dash

Can someone please tell me how I can make my player dash? When I dash, the hitbox doesn't follow the player, which prevents collisions and other issues. What can I do? Here's my code.

updatePlayer = function()
  // 1. GESTION DU DASH (AU DÉBUT)
  if keyboard.press.H and player.energie > 40 and not player.dash then
    player.dash = true
    player.dash_timer = 10 // Durée du dash
    player.energie -= 40
  end

  // Si le dash est actif
  if player.dash then
    local dash_speed = player.vitesse * 4
    if player.direction == 1 then player.y += dash_speed player.image = "dash_up" end
    if player.direction == -1 then player.y -= dash_speed player.image = "dash_down" end
    if player.direction == 2 then player.x -= dash_speed player.image = "dash_left" end
    if player.direction == -2 then player.x += dash_speed player.image = "dash_right" end
    
    player.dash_timer -= 1
    if player.dash_timer <= 0 then player.dash = false end
    
    // On quitte la fonction ici pour ne pas faire le déplacement normal pendant le dash
    return 
  end


  // 3. RÉGÉNÉRATION VIE ET ÉNERGIE
  if player.vie > 0 and player.vie < player.vie_max then
    player.vie += 0.05
  end
  
  if player.vie <= 0 then player.alive = false end

  // 4. DÉPLACEMENT NORMAL
  local move = false 
  oldX = player.x
  oldY = player.y

  if player.alive and player.canMove then
    if keyboard.UP then
      player.y += player.vitesse 
      player.direction = 1
      player.image = "up"
      move = true
      player.energie -= 0.2
    elsif keyboard.DOWN then 
      player.y -= player.vitesse
      player.direction = -1
      player.image = "down"
      move = true
      player.energie -= 0.2
    elsif keyboard.LEFT then 
      player.x -= player.vitesse
      player.direction = 2 // 2 pour Gauche selon ton code
      player.image = "left"
      move = true 
      player.energie -= 0.2
    elsif keyboard.RIGHT then 
      player.x += player.vitesse 
      player.direction = -2 // -2 pour Droite selon ton code
      player.image = "right"
      move = true
      player.energie -= 0.2
    end
  end

  if not move and not player.dash then
    player.image = "idle"
    player.energie += 0.5
  end

  // 5. MISE À JOUR SYSTÈME
  Player_hitbox.x = player.x
  Player_hitbox.y = player.y
  Energie()

from what I can see, you are checking if you can dash without checking if you can even go there. I'd suggest something along the lines of only applying momentum if there is nothing in the way. could you let us know what kind of collision you use?

I'm using collision with a list class for game fluidity. I've created a hitbox for the player; here's the code.

Solide = class
  constructor = function(x,y,width,height)
  this.x = x
  this.y = y
  this.width = width
  this.height = height
  end
end

list_solide = []
list_solide.push(new Solide(70, 70, 30, 30))

checkCollision = function(r1, r2)
  return r1.x - r1.width/2 < r2.x + r2.width/2 and
         r1.x + r1.width/2 > r2.x - r2.width/2 and
         r1.y - r1.height/2 < r2.y + r2.height/2 and
         r1.y + r1.height/2 > r2.y - r2.height/2
end

updateSolide = function()
  for s in list_solide
    if checkCollision(s, Player_hitbox) then
      player.x = oldX
      player.y = oldY
      Player_hitbox.x = oldX
      Player_hitbox.y = oldY
    end
  end
end

drawSolide = function()
  for s in list_solide
    screen.drawRect(s.x - camera.x, s.y - camera.y, s.width, s.height, "rgb(0,0,255)")
  end
end

The problem is that when I dash, the collision hitbox doesn't follow the player during the action. So if I'm in front of the dash, flying over a wall (since the collision hitbox no longer follows the player, there's no structure to stop me), and the dash ends, I get stuck in it. It's only when the dash is over that the hitbox returns to the player's coordinates, and I don't know why.

what i'm noticing is that instead of stopping the player from moving, you're just teleporting them back. when you dash it is moving the player, then updating the old x and y. this would mean if you're teleported back to the old x and y, it would just be where you already are. that's my best guess as to the reason. try updating the old x and y before dashing.

ok thanks

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community