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

Mining Stone

A

Arthod

Guest
I'm looking for a way. If I hit "Q" while playing as an object then it checks if theres is a special object infront of me, atleast 100 pixels away. If there is then destroy that object and give the player I'm plaing "a variable" "var += 3" etc. If anyone is able to help me, please do. Thanks.

I'm doing a game where your image angle is your direction, so basically if anything in front of you.. as in image angle infront.
 
J

Jordan Robinson

Guest
There are a few different ways that you could go about doing this. I will give you an example of a reasonably simple way.

The method I will show you uses a collision check called collision_line(). You can get more information about it here. Using the collision line, we can check for the object we wish to destroy in the direction the player is facing with a length of 100px. The collision check will return the ID of the instance if it finds a collision, or it will return the keyword noone if it didn't find a collision (noone = -4).

Code:
if(keyboard_check_pressed(ord('Q')))
{
    var end_x = x + lengthdir_x(100, image_angle);
    var end_y = y + lengthdir_y(100, image_angle);
    var object = collision_line(x, y, end_x, end_y, obj_object, false, true));

    if(object != noone)
    {
        some_variable += 3;
        with(object)
        {
            instance_destroy();
        }
    }
}

This code isn't tested so it may need a few tweaks if you copy / paste it. Also you would need to change the variable names to suit your game.

The variables end_x and end_y are the x and y position 100 pixels in front of the player. The collision line will check for a collision with the object between its own x and y and the end_x and end_y.

Hope this helps.
 
Last edited by a moderator:
A

Arthod

Guest
How would I check where the player is looking though? I want to check the stuff exactly in front of his eyes. (image_angle) do you think there's a way?
 
A

Arthod

Guest
There are a few different ways that you could go about doing this. I will give you an example of a reasonably simple way.

The method I will show you uses a collision check called collision_line(). You can get more information about it here. Using the collision line, we can check for the object we wish to destroy in the direction the player is facing with a length of 100px. The collision check will return the ID of the instance if it finds a collision, or it will return the keyword noone if it didn't find a collision (noone = -4).

Code:
if(keyboard_check_pressed(ord('Q')))
{
    var end_x = lengthdir_x(100, image_angle);
    var end_y = lengthdir_y(100, image_angle);
    var object = collision_line(x, y, end_x, end_y, obj_object, false, true));

    if(object != noone)
    {
        some_variable += 3;
        with(object)
        {
            instance_destroy();
        }
    }
}

This code isn't tested so it may need a few tweaks if you copy / paste it. Also you would need to change the variable names to suit your game.

The variables end_x and end_y are the x and y position 100 pixels in front of the player. The collision line will check for a collision with the object between its own x and y and the end_x and end_y.

Hope this helps.
Oh thanks, I'll check it out!
 
A

Arthod

Guest
There are a few different ways that you could go about doing this. I will give you an example of a reasonably simple way.

The method I will show you uses a collision check called collision_line(). You can get more information about it here. Using the collision line, we can check for the object we wish to destroy in the direction the player is facing with a length of 100px. The collision check will return the ID of the instance if it finds a collision, or it will return the keyword noone if it didn't find a collision (noone = -4).

Code:
if(keyboard_check_pressed(ord('Q')))
{
    var end_x = lengthdir_x(100, image_angle);
    var end_y = lengthdir_y(100, image_angle);
    var object = collision_line(x, y, end_x, end_y, obj_object, false, true));

    if(object != noone)
    {
        some_variable += 3;
        with(object)
        {
            instance_destroy();
        }
    }
}

This code isn't tested so it may need a few tweaks if you copy / paste it. Also you would need to change the variable names to suit your game.

The variables end_x and end_y are the x and y position 100 pixels in front of the player. The collision line will check for a collision with the object between its own x and y and the end_x and end_y.

Hope this helps.
When I draw this line, it dosen't seem to work the way I want it. I want the line to start from the player, and go towards where he is looking. What I mean is it's not drawing the line as you expected, it isn't a hundred pixel from the player towards his image_angle
 
S

Snail Man

Guest
That's how it should work, it's possible the line just isn't pointing the right direction. Try changing image_angle to image_angle+90, -90, or +180 to see if the line is just coming out of the side or bottom by accident?
 
A

Arthod

Guest
I realized I needed x+ and y+ behind the lengthdir_y(100, image_angle) and lengthdir_x(100, image_angle), thanks for the help!
 
Top