• 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!

Legacy GM Enemy collision similar to player's?

A

AwesomeAlvin

Guest
So right now, this is my player's movement and collision code for a top down shooter:
Code:
hsp = walkSpeed * (keyboard_check(ord('D')) - keyboard_check(ord('A')));
vsp = walkSpeed * (keyboard_check(ord('S')) - keyboard_check(ord('W')));

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

if (place_meeting(x,y+vsp, obj_wall)) {
    while (!place_meeting(x,y+sign(vsp),obj_wall)) {
        y += sign(vsp);
    }
    vsp = 0;
}
y += vsp;
It's pretty much Shaun Spalding's one. However, how do I implement this for my enemies? How would I be able to capture their horizontal and vertical speed. Would I just have to use hspeed or vspeed, or is there a way to use the custom speed variables for this?

If I were to change the variables from the customs and use the built it, it would look like this for the enemies:
Code:
if (place_meeting(x+hspeed,y, obj_wall)) {
    while (!place_meeting(x+sign(hspeed),y,obj_wall)) {
        x += sign(hspeed);
    }
    hspeed = 0;
}

if (place_meeting(x,y+vspeed, obj_wall)) {
    while (!place_meeting(x,y+sign(vspeed),obj_wall)) {
        y += sign(vspeed);
    }
    vspeed = 0;
}
But I find it weird that my player is using vsp and hsp while enemies are using vspeed and hspeed. Should that be fine though?

And also, I want my enemies to be able to collide with each other; how can I do this by using place_meeting?

Any help would be appreciated, thanks!
Let me know if there's any more information I can provide.
 

matharoo

manualman
GameMaker Dev.
hsp and vsp are variables you created.
hspeed and vspeed are in-built variables that are already there for every object.
There's no rule specifying what you should use. It all depends on how you have built your object's code.
So, your player uses hsp and vsp. Well, that's how you've programmed it. It's totally fine.
Now to your enemy object - how does it move? How is its movement code built? Does it also use hsp and vsp variables to move? If so, you can just use those.
Or if not, you can just use hspeed and vspeed which IIRC automatically catch the object's 2D speeds.

For your next question, I think you can use place_meeting (just specify the enemy object in its third argument).
For example:
Code:
if (place_meeting(x+hspeed, y+vspeed, obj_enemy)) {//stop}
 
A

AwesomeAlvin

Guest
hsp and vsp are variables you created.
hspeed and vspeed are in-built variables that are already there for every object.
There's no rule specifying what you should use. It all depends on how you have built your object's code.
So, your player uses hsp and vsp. Well, that's how you've programmed it. It's totally fine.
Now to your enemy object - how does it move? How is its movement code built? Does it also use hsp and vsp variables to move? If so, you can just use those.
Or if not, you can just use hspeed and vspeed which IIRC automatically catch the object's 2D speeds.

For your next question, I think you can use place_meeting (just specify the enemy object in its third argument).
For example:
Code:
if (place_meeting(x+hspeed, y+vspeed, obj_enemy)) {//stop}
Thanks for the explanation of those variables.

For the collisions of the enemies, I still have no clue to code those collisions with more than one object. With your example code, I don't know what to put in there, sorry; I'm still a bit new to gamemaker :/.
I had a collision code to work with solids, and just enable solids to enemies, but I heard that using the built in solids checkbox isn't recommended, so I want to stay away from it.
 

TheouAegis

Member
Just use two separate collision checks just like you have in the player.

If horizontal collision with wall { }
else
if horizontal collision with enemies { }
 

matharoo

manualman
GameMaker Dev.
Thanks for the explanation of those variables.

For the collisions of the enemies, I still have no clue to code those collisions with more than one object. With your example code, I don't know what to put in there, sorry; I'm still a bit new to gamemaker :/.
I had a collision code to work with solids, and just enable solids to enemies, but I heard that using the built in solids checkbox isn't recommended, so I want to stay away from it.
You can make those enemy objects children of a parent object and use that parent object in your collision code.
For example, create an object called "obj_enemy_parent" or something. Open your enemy objects and set their parent to the parent object you just created. In your collision code, check for obj_enemy_parent.
Read more about parents: Parents
 
Top