GameMaker [SOLVED] Why Won't This Code Work?

Gamerev147

Member
I'm using the same movement system from my other top down shooters for my new engine. It's just a test movement system and will be changed in the future. But as of now I need to use it and it won't work in GMS2.
Could someone explain why this won't work?

Code:
hspeed = spd * (keyboard_check(global.KEY_RIGHT) - keyboard_check(global.KEY_LEFT));
vspeed = spd * (keyboard_check(global.KEY_DOWN) - keyboard_check(global.KEY_UP));

//Collision
if (collision_circle(x + lengthdir_x(6, direction) + hspeed, y + lengthdir_y(6, direction) - vspeed, 32, obj_Wall, 1, 1)) {
    hspeed = 0;
}

if (collision_circle(x + lengthdir_x(6, direction) - hspeed, y + lengthdir_y(6, direction) + vspeed, 32, obj_Wall, 1, 1)) {
    vspeed = 0;
}
Yes, I've tried the obvious... I get no errors...
My problem is that the player won't collide with the walls. Please help!
 
B

Badger

Guest
Using lengthdir functions like this might help.
Code:
x + lengthdir_x(6+ hspeed, direction)
 

NightFrost

Member
I'm not understanding why you switch the plus and minus sign in front of your speed variables in the checks. This makes it test different places for the horizontal and vertical tests. Neither do I get what the magic number six does in the check. You're also just setting the speed variable to zero, so if the collision code otherwise worked perfectly, you'd be stopping some distance away from the actual collider. You didn't mention what doesn't work, but I assume you completely stop moving when collision is registered, as you have no way of getting out of collision after the fact. (On the other hand, horizontal and vertical checks looking at different places may be preventing you getting completely stuck.)
 

Gamerev147

Member
I'm not understanding why you switch the plus and minus sign in front of your speed variables in the checks. This makes it test different places for the horizontal and vertical tests. Neither do I get what the magic number six does in the check. You're also just setting the speed variable to zero, so if the collision code otherwise worked perfectly, you'd be stopping some distance away from the actual collider. You didn't mention what doesn't work, but I assume you completely stop moving when collision is registered, as you have no way of getting out of collision after the fact. (On the other hand, horizontal and vertical checks looking at different places may be preventing you getting completely stuck.)
I didn't ask for critique. I asked if someone could please help me figure out why this code doesn't work in Game Maker Studio 2. It worked in Game Maker Studio 1.4.
I also did clearly state at the bottom of my thread that my issue is that the player will not collide with the walls.

I made this code because it's what works for me. I'm sorry if it makes you upset, but that's not what I asked for.
 
C

Crazy Star

Guest
It's a good critique. Anyway, I'd maybe have done it like this:

Code:
hspeed = spd * (keyboard_check(global.KEY_RIGHT) - keyboard_check(global.KEY_LEFT));
vspeed = spd * (keyboard_check(global.KEY_DOWN) - keyboard_check(global.KEY_UP));

with(obj_Wall) {
    var r;
        r = 32+32; // Radius for both objects
    var dif;
        dif = point_distance(x, y, other.x, other.y);
    if(dif<r) {
        var dx, dy, dif;
            dx = (other.x-x)/dif;
            dy = (other.y-y)/dif;
        other.x = x + dx*r;
        other.y = y + dy*r;
        other.hspeed *= 0.92;
        other.vspeed *= 0.92;
    }
}
 

Gamerev147

Member
Thank you @Crazy Star !
I used your code and changed a few things. It works great!

I also figured out why my old code didn't work... It wasn't a matter of the way the code was written, but where I wrote it in the event.
Strange... I know.

Thank you to everyone who helped! :)
 
Top