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

How to make object attached to an object disappear

TJKeets

Member
Hi,

I'm tying to make an enemy and a gun only become visible when within a certain range of my player.

The enemy works fine but not the gun. I've tried many different things but I either get errors or guns being drawn at every position the enemy moves.

The gun object is created at the create event for the enemy.

Code:
gun = instance_create_layer(x, y, "Weapons", oPistol);
In the step event I have this code:
Code:
with(gun)
{
x = other.x;
y = other.y;

}
I have this code in the draw event for my enemy:

Code:
if(instance_exists(oPlayer))
{
    if(distance_to_object(oPlayer)<150)
    {
    draw_self();
    }
}
What would be the best way to make the gun disappear too?

Thanks in advance,

Tom
 

FrostyCat

Redemption Seeker
If you simply want things to disappear, just use visible, no need for separate Draw event code.
GML:
visible = instance_exists(oPlayer) && distance_to_object(oPlayer) < 150;
with (gun)
{
    x = other.x;
    y = other.y;
    visible = other.visible;
}
 

Yal

šŸ§ *penguin noises*
GMC Elder
GML:
with(gun) {
  x = other.x;
  y = other.y;
  visible = other.visible;
  image_alpha = other.image_alpha;
}
 
Top