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

GameMaker [SOLVED] Bullet Object Only Destroyed Sometimes

S

Sarena

Guest
I created a character that uses a gun weapon, and whenever you right-click, it shoots a bullet projectile towards the position that you clicked in. I created it in a similar fashion to how I made the player move to a spot you click by left-clicking.

The system almost works completely how I want it to, but sometimes when I am shooting, the bullet object will stop on the wall and not disappear like it is supposed to.

Here's the code for my objPlayer's Step Event (involving the gun):
Code:
// Assign Variable
attack_input = mouse_check_button_pressed(mb_right);

// If Expression
if(attack_input)
{

        // Assign Variable
        firing_delay = firing_delay - 1;
    
        // If Expression
        if((mouse_check_button(mb_right)) and firing_delay < 0)
        {
            // Assign Variable
            firing_delay = 1;
            recoil = 4;
        
            // Apply To
            with(instance_create_layer(x, y, "Projectile", objProjectile)) {
                // If Instance Exists
                var l624DA0FF_0 = false;
                l624DA0FF_0 = instance_exists(objGoalWeapon);
                if(l624DA0FF_0)
                {
                    // Destroy Instance
                    with(objGoalWeapon) instance_destroy();
                }
            
                // Create Instance
                instance_create_layer(mouse_x, mouse_y, "Instances", objGoalWeapon);
            
                // Assign Variable
                PathWeapon = path_add();
            
                // Assign Variable
                direction = PathWeapon;
            
                // Function Call
                mp_linear_path(PathWeapon,objGoalWeapon.x, objGoalWeapon.y, 20, false);
            
                // Start Following Path
                path_start(PathWeapon, 20, path_action_stop, true);
            
                // Set Speed
                speed = 25;
            
                // Set Direction Variable
                direction = other.image_angle + random_range(-3, 3);
            
                // Set Instance Rotation
                image_angle += direction;
            }
        
            // Assign Variable
            recoil = max(0,recoil - 1);
        
            // Jump To Point
            x = x - lengthdir_x(recoil, image_angle);
            y = y - lengthdir_y(recoil, image_angle);
        }
    }
For the objProjectile, I added the following code to a Collision with objWall (and objGoalWeapon) Events:
Code:
// Destroy Instance
with(objGoal) instance_destroy();

// Destroy Instance
instance_destroy();
The projectile seems to only get stuck when I click on the wall itself. My guess is that it has something to do with the path not being completed, so it might need an action for destroying itself if the path it is interrupted and unable to complete, but I'm not sure how to go about setting that up.
 

Bentley

Member
This might be a bit off topic. I saw that you're making your bullet follow a path. If it's some sort of "smart" bullet that goes around corners, I can see why you're creating a path for it. But if it just goes in the direction you click, a path might be making it more complicated than necessary.
Sorry if this isn't helpful.

Also, you're creating a lot of paths. Are you destroying them?
 
S

Sarena

Guest
This might be a bit off topic. I saw that you're making your bullet follow a path. If it's some sort of "smart" bullet that goes around corners, I can see why you're creating a path for it. But if it just goes in the direction you click, a path might be making it more complicated than necessary.
Sorry if this isn't helpful.

Also, you're creating a lot of paths. Are you destroying them?
This was the best method I was able to figure out to that worked for sending the object to the specific place that is clicked on the screen.
Do you have a simpler recommendation for how I could accomplish this?

Also, I haven't written the code yet for deleting the paths yet, but I will once I get this portion working.
 

TheouAegis

Member
Instead of a collision event, try just putting a place_meeting check in the end step event.


But for linear motion, it's just

Code:
with(instance_create_layer(x, y, "Projectile", objProjectile)) {
speed = 25;
direction = point_direction(x,y,mouse_x,mouse_y);

//If you want the bullet to be destroyed at the mouse position...
goalx=mouse_x;
goaly=mouse_y;
}
Then in the bullet's End Step event...
Code:
if collision_line(xprevious,yprevious,x,y,objWall,0,0)
instance_destroy();

if sign(point_direction(x,y,goalx,goaly)-180) != sign(direction-180) 
instance_destroy();
 

Slyddar

Member
The variable l624DA0FF_0 seems awfully hard to remember and type again and again. Any reason you are using such a complex variable name?
 
S

Sarena

Guest
Instead of a collision event, try just putting a place_meeting check in the end step event.


But for linear motion, it's just

Code:
with(instance_create_layer(x, y, "Projectile", objProjectile)) {
speed = 25;
direction = point_direction(x,y,mouse_x,mouse_y);

//If you want the bullet to be destroyed at the mouse position...
goalx=mouse_x;
goaly=mouse_y;
}
Then in the bullet's End Step event...
Code:
if collision_line(xprevious,yprevious,x,y,objWall,0,0)
instance_destroy();

if sign(point_direction(x,y,goalx,goaly)-180) != sign(direction-180)
instance_destroy();
I was just playing around and found a way to simplify the code I had so that a path wasn't needed after all.
Thank you for your help, this helps for where I do need paths!

The variable l624DA0FF_0 seems awfully hard to remember and type again and again. Any reason you are using such a complex variable name?
I'm using Drag and Drop, so it generated that name on its own for if an instance exists.
 

Bentley

Member
This was the best method I was able to figure out to that worked for sending the object to the specific place that is clicked on the screen.
Do you have a simpler recommendation for how I could accomplish this?

Also, I haven't written the code yet for deleting the paths yet, but I will once I get this portion working.
EDIT: nm, I was too late : (
 
Top