I am really short on time all things considered so please help! I need to spawn asteroids in the direction the player is facing with a randomized margin. The current spawning code is as follows (1 line): instance_create_layer(random_range(objPlayer2.x+100, objPlayer2.x-100),random_range(objPlayer2.y+100, objPlayer2.y-100),"Instances",objAsster);How do I make it spawn in the right place, and is this code just going to be gross or is there a way to shorten it?
#Random spawning in direction player is facing
1 messages · Page 1 of 1 (latest)
so like you want that +100/-100 range to be 0-200 in the direction the player is facing instead?
exactly
gotcha, you'll need some trig for that
although ideally more than 200 so it doesnt look like its just appearing
do you want it to be exactly the direction they are facing, or with a random direction offset?
like +- 45 degrees or what have you
random offset yes
gotcha, lemme whip up an example
this'll also show how you can shorten the big line in the process
//get the random distance
var randRange = random_range(100, 200);
//get a random direction
var randDir = image_angle + random_range(-45, 45);
//use lengthdir to turn those into actual offsets that can be added to x and y
var offsetX = lengthdir_x(randRange, randDir);
var offsetY = lengthdir_y(randRange, randDir);
instance_create_layer(x + offsetX, y + offsetY,"Instances",objAsster);```
by using lengthdir, we get an offset that's based on a distance and a direction
oh wait I forgot to factor in the player's facing angle
is that based on image_angle?
yes it is!
there we go edited, just added it to the direction
alr I'll report back
you can edit the 100, 200, 45, etc. to adjust things
oop, i got an error
I definitely could have made a mistake somewhere since I typed it straight into discord
what's the error?
oh my bad, it's becuase i had to change the object name to post here becuase im unfamiliar with the rules on swear words and i shortened the word further
okay, the error is gone but now it doesn't appear to be spawning any
to be fair i did put it in an if statement
What event is this code in? Is it in a different place from your previous instance_create_layer code?
no, it's in the step event for objGame which just handles things like timer
var randRange = random_range(100, 200);
var randDir = objPlayer2.image_angle + random_range(-45, 45);
var offsetX = lengthdir_x(randRange, randDir);
var offsetY = lengthdir_y(randRange, randDir);
instance_create_layer(x + offsetX, y + offsetY,"Instances",objAss);
diffThresh++;
}```
i did make the image angle that of the player object
gotcha, I assumed this code was on the player
right now the base x and y it's spawning at is that of objGame
ohhhhhhhhh
so you also need to change that to be based on the player object
okay, now they're spawning closer, but they're behind the player
hmm is your player's sprite backwards maybe?
wondering if image_angle is reverse of what you'd expect
that is... possible?
well the quick fix is to add a +180 to the angle to make it go the other way
you said you're short on time so perhaps the quick fix is the way to go lol
i did have some sprite and direction confusion at the start of the project so that's likely it
yeah in GM, a direction of 0 is facing right
so if your sprite isn't facing right, things will be offset weirdly
that explains so much actually
well you can choose between slapping in a +180, or rotating your sprite and removing your other workarounds haha
also they are spawning correctly now, but if i wanna make them further out i just have to add to the 100, 200 range right?
yeah, you can change those numbers to be whatever you like
the -45 and 45 can also be edited to change how much of an angle spread there is
the 100 is the minimum range, 200 is the max
that's it! exactly what i was hoping for :)