• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

create instances only in specific area

TRP

Member
Hi there!

Having a bit of trouble making objects appear only in a certain area. Like below image

I'm using this:

instance_create_layer(random(room_width -500),random(room_height -500),"Instances",obj_enemy);

They create but everywhere. Even at the very edges of the room. I'm obviously missing something.

Thanks for any help!
question.png
 

TsukaYuriko

🌠
Forum Staff
Moderator
Right now you're only excluding the right border (room width minus 500 on the right) from the horizontal spawning... you'd also have to exclude the left border (leftmost point of the room plus 500). ;)
 

saffeine

Member
you're forgetting to offset from the top / left too. right now you're just subtracting from the right / bottom.

GML:
//    declare your bounds here.
var _top      = 400;
var _left     = 200;
var _right    = 800;
var _bottom   = 800;

//    now get the differences between the points, that's your effective spawn area.
//    once you've gotten the random position in that area, add the minimum positions.
var _x = random( _right - _left ) + _left;
var _y = random( _bottom - _top ) + _top;

//    spawn the object.
instance_create_layer( _x, _y, "Instances", obj_enemy );
edit: Tsuka was much quicker than me it seems. oh well, there's always next time 😔
 

Yal

🐧 *penguin noises*
GMC Elder
Try using random_range to have an easier time computing the limits:
GML:
instance_create_layer( random_range(500,room_width - 500), random_range(500,room_height - 500) , "Instances", obj_enemy );
 
Top