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

GML Laser obstacle

zampa

Member
I wanted to add a laser as an obstacle, basically you would have the laser emitter and the laser it self that goes on until it hits a wall, and i was wondering how to implement that last thing.

My tiles are 32x32 so my idea was to run in a while statement a tilemap_get_at_pixel that checks that pixel if there is a wall, i would increment the position too check by 31-32 every time until it finds the wall, at that point i would check backwards to find the "first" pixel of the tile, at this point i would know the distance between the emitter and the wall, know it's only a matter to strech the laser sprite itself (the sprite is 19x1) until it touches the wall.

Is that a good way to do it?
How would you do it?

thanks :)
 

YoSniper

Member
Here's an example that I would use.

ASSUMPTIONS:
1. The laser has a collision mask of 32x32 just like any of the tiles.
2. There are Wall objects on top of whatever tiles you are using that act as borders.
3. A horizontal laser going to the right has its x origin on the far left of the sprite.

For a Horizontal Laser Generating to the Right:
Create Event

Code:
while not place_meeting(x + 1, y, obj_wall) and image_xscale < 100 {
    //The second condition is to prevent an infinite loop from happening
    image_xscale += 1;
}
 
Top