SOLVED? Getting stuck in Wall

Velocity

Member
Hey,

So I'm making a fighting game and my character is getting stuck in the walls.

I set my player object mask as a sprite, so that the sprites wouldn't change and stick me into the wall.

Also, I made sure that sprite had an odd number of pixels and set the origin to the exact pixel horizontally in the middle of the sprite.

Plus, I also used Shaun Spalding's video tutorial about platformer basics, and set a collision checker preventative measure against getting stuck in the walls.

I even used another solid object (placed over the wall object) and used a normal collision event between that and the player object.

I'm wondering if it's because Game Maker isn't recognising a variable I made global, called "global.playerspeed" instead of hsp.

By the way, I'm making my game with someone else's code. He also had a couple of problems with getting stuck in the walls, but only the opponent used to get stuck when he moved back into the wall after getting hit. I also changed his sprites to my new ones.

So, here's my code:

//Horizontal collision to avoid getting stuck in wall
if (place_meeting(x + global.playerspeed, y, obj_player_parent))
{
while (!place_meeting(x+sign(global.playerspeed),y,obj_player_parent))
{
x += sign (global.playerspeed) ;
}
global.playerspeed = 0;
}
 

Velocity

Member
The most common cause of characters getting stuck in platformer walls is using animated sprites without a static collision mask.

Make a solid-fill, one-frame rectangular sprite with the same size and origin as your character, then go into Object Properties and set Mask to it.
Thanks for the comment. Yeah, I did that already. Except, I don't think there is an "origin for my character", because my character object is a parent with no sprite attached to it.
 
G

Gre_Lor12

Guest
Well at this point you can do 2 simple things (with various others):

  1. override the parent by putting in the "create event" the "Inherit the parent event" block and then create a small script with: sprite_index = [sprite_name] that will then set an origin ;)
  2. Create a collision box using collision_rectangle(x1, y1, x2, y2, prec, notMe):
var coll = [object]
//Horizontal collision to avoid getting stuck in wall
if (collision_rectangle(x + global.playerspeed, y, x, y, false, true) )
if (place_meeting(x + global.playerspeed, y, obj_player_parent) )
{
while (!place_meeting(x+sign(global.playerspeed),y,obj_player_parent))
{
x += sign (global.playerspeed) ;
}
global.playerspeed = 0;
}


This is only a rough kinda script so I am not sure if it will work in that format so you may have to throw it about a bit. ;/
 
Last edited by a moderator:
G

Gre_Lor12

Guest
Either that or take out the global.playerSpeed = 0; and change the while a little to add an else in to set the playerspeed to 0 when needed
 

Yal

🐧 *penguin noises*
GMC Elder
Except, I don't think there is an "origin for my character", because my character object is a parent with no sprite attached to it.
Do all the children use the mask as well, or just the parent?
 

Velocity

Member
Do all the children use the mask as well, or just the parent?
At first I applied the mask to the main child that I'm working on (my only character that's being fleshed out first), then I applied it to the parent. I tried both.
 

Velocity

Member
Well at this point you can do 2 simple things (with various others):

  1. override the parent by putting in the "create event" the "Inherit the parent event" block and then create a small script with: sprite_index = [sprite_name] that will then set an origin ;)
  2. Create a collision box using collision_rectangle(x1, y1, x2, y2, prec, notMe):
var coll = [object]
//Horizontal collision to avoid getting stuck in wall
if (collision_rectangle(x + global.playerspeed, y, x, y, false, true) )
if (place_meeting(x + global.playerspeed, y, obj_player_parent) )
{
while (!place_meeting(x+sign(global.playerspeed),y,obj_player_parent))
{
x += sign (global.playerspeed) ;
}
global.playerspeed = 0;
}


This is only a rough kinda script so I am not sure if it will work in that format so you may have to throw it about a bit. ;/
Okay, so where do I put the collision box code? I tried putting that in the create event for the parent as well and got a red error. I'm pretty new to Game Maker by the way.
 

Yal

🐧 *penguin noises*
GMC Elder
What's the error message? Copy the text and paste it here.
 

Velocity

Member
Okay, so I kind of solved this, but not really, but kinda did.

I simply remade the game again, and the problem didn't happen anymore.

I didn't really figure out how to fix the problem, but rather just found a way around it.

Should I mark this thread as solved? :confused:
 
G

Gre_Lor12

Guest
Nah place it in the Step Event.

The Create Event only runs things once aka - when the object is created.

The Step event runs it every step. Now the number of steps is equal to your room speed. And your room speed is also your max FPS.

So if you have a room speed of 30, that means your max FPS is 30 and also there are 30 steps in a second
 
G

Gre_Lor12

Guest
The create event can be used for "ifs" but it will only check it one. If you want something always checking, put it in the step event.

Begin Step and End Step are there too, but I doubt that you need them in this case
 
C

cboyd224

Guest
Well at this point you can do 2 simple things (with various others):

  1. override the parent by putting in the "create event" the "Inherit the parent event" block and then create a small script with: sprite_index = [sprite_name] that will then set an origin ;)
  2. Create a collision box using collision_rectangle(x1, y1, x2, y2, prec, notMe):
var coll = [object]
//Horizontal collision to avoid getting stuck in wall
if (collision_rectangle(x + global.playerspeed, y, x, y, false, true) )
if (place_meeting(x + global.playerspeed, y, obj_player_parent) )
{
while (!place_meeting(x+sign(global.playerspeed),y,obj_player_parent))
{
x += sign (global.playerspeed) ;
}
global.playerspeed = 0;
}


This is only a rough kinda script so I am not sure if it will work in that format so you may have to throw it about a bit. ;/
just wondering if someone can give me an answer that works with the drag and drop method on game maker studio 2
 
C

cboyd224

Guest
Hey everyone im trying to learn gms2 and i was wondering if someone can reply with and answer to this question using the drag and drop option to prevent a player from getting stuck on a wall
 
C

cboyd224

Guest
for anyone thats using the drag and drop version, this is a way i figured out with getting stuck on a wall if you want to know

alright i figured it out, i had to take what i was watching on a video on youtube and try to apply it to the drag and drop version, i was confused only because the drag and drop version doesn't explain the variable and actions very well, like throwing in the action "key down right" for explain is like a if variable for that key,

so in that action of "key down right"
- i put in the "if expression" (place_free(x + colspd, y) )
//in "create" i had (var colspd = walkspd +2) where just so you know var walkspeed = 4 and colspd means collisionspeed//

-followed by an "if variable" (x greater or equal walkspd) then followed by
-"jump to point" x=walkspd "relative" y=_
-with the set sprite after it for the player to look in that direction
 

Attachments

Yal

🐧 *penguin noises*
GMC Elder
just wondering if someone can give me an answer that works with the drag and drop method on game maker studio 2
Just FYI, drag-and-drop is much more cumbersome to share than code (as you probably noticed when you had to type everything out - with code, you'd just needed Ctrl-A, Ctrl-C, Ctrl-V and boom, post done) so nobody that shares code on a regular basis will give you DnD... it just takes so much longer :p

(Not to mention code has a bunch of other benefits, including being faster to create - you don't need to drag it around, and you can type out any command you're aware exists without having to find it in a list first)
 
Just FYI, drag-and-drop is much more cumbersome to share than code (as you probably noticed when you had to type everything out - with code, you'd just needed Ctrl-A, Ctrl-C, Ctrl-V and boom, post done) so nobody that shares code on a regular basis will give you DnD... it just takes so much longer :p

(Not to mention code has a bunch of other benefits, including being faster to create - you don't need to drag it around, and you can type out any command you're aware exists without having to find it in a list first)
It's possible to share the code of a drag and drop event by converting it to GML but once it's GML u can't convert it back to D&D so I would recommend you make a new event you don't need with same drag and drop "code" and convert that to GML to share. lol. an "expendable" event if you will
 
I know this is not a problem anymore, but I maybe can help you understand the problem.
What happened to me was that there was another object that could manipulate the player's speed other than the player itself. This made so that, as the other objects events were read later in the frame, the speed was applied after the collision code.
The solution was to instead of the other object directly manipulating the player's speed, it set a variable that would be applied to the actual speed in the player's step event, before the collision part of the code. So a frame would go like this:

obj_player Step Event:
1. global.playerspeed += other_obj_applied_speed
2. Collision code
3. x += global.playerspeed
4. other_obj_applied_speed = 0

The other object's Step Event:
1. if condition to apply the extra speed is met:
2. obj_player.other_obj_applied_speed = speed_to_apply

I know the variable names are too long, but this is just so it'd be easy to understand exactly what each one serves for.
But now that I think of it, you could just put the collision code and the "x += global.player_speed" in the End Step Event. But oh well, in my game I had using the built-in speed variables, that are applied to the position before the End Step Event, so I had to use this workaround that creates a one frame delay to adding that extra speed.
 
Last edited:
Top