• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Pixel Collision with Sprint Button?

N

Necromedes

Guest
I've started work on a top-down, action rpg of sorts. I'm using Shaun's platformer code as a base until I finish watching his tutorial on action rpgs.
I have the code set up for horizontal and vertical collisions and those work just fine. However, I have set up a sprint/dodge key that increases the players move speed multiplier for a moment and then, using a coded timer, it resets to the regular value.

That being said, I have found that when the movement speed multiplier is increased, it sort of breaks the collision code. I was hoping that since it was simply modifying the existing code on the fly, the engine would keep up and work it out but apparently not. Instead, if I move to collide with a wall object while sprinting, it ends with the player overlapping the object.

Now, I considered using xprevious or something like that to see if it would fix it but I figured I'd come here and get some opinions on how I could adjust this to make it work first, instead of wasting a ton of time and bashing my head against the keyboard.

Here's the code I'm working with:


key_W = keyboard_check(ord("W"))
key_A = keyboard_check(ord("A"))
key_S = keyboard_check(ord("S"))
key_D = keyboard_check(ord("D"))


if sprint_timer > 0{
sprint_timer -= 2
}

if keyboard_check_pressed(vk_space) && sprint_timer <= 0{
sprint_timer = 30
}

if sprint_timer > 0{
move_speed = 4
}
else{
move_speed = 2
}

hmove = key_D - key_A
vmove = key_S - key_W


hsp = hmove * move_speed
vsp = vmove * move_speed

if (place_meeting(x+sign(hsp), y, obj_wall)){
if (!place_meeting(x+sign(hsp), y, obj_wall)){
x = x + sign(hsp)
}
hsp = 0
}

if (place_meeting(x, y+sign(vsp), obj_wall)){
if (!place_meeting(x, y+sign(vsp), obj_wall)){
y = y + sign(vsp)
}
vsp = 0
}

x = x + hsp
y = y + vsp
 
Do you work with a low resolution project? This collision check isn't pixel perfect and so on very low resolution projects, you can see the player overlapping a bit.
 
N

Necromedes

Guest
Ah, I placed if instead of while. Regardless, the problem still persists when used close to a wall. It isn't nearly as far as it was before, however.

EDIT: Solution found.
 
Last edited by a moderator:
Top