#Random spawning in direction player is facing

1 messages · Page 1 of 1 (latest)

quaint nimbus
#

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?

ocean seal
#

so like you want that +100/-100 range to be 0-200 in the direction the player is facing instead?

quaint nimbus
#

exactly

ocean seal
#

gotcha, you'll need some trig for that

quaint nimbus
#

although ideally more than 200 so it doesnt look like its just appearing

ocean seal
#

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

quaint nimbus
#

random offset yes

ocean seal
#

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?

quaint nimbus
#

yes it is!

ocean seal
#

there we go edited, just added it to the direction

quaint nimbus
#

alr I'll report back

ocean seal
#

you can edit the 100, 200, 45, etc. to adjust things

quaint nimbus
#

oop, i got an error

ocean seal
#

I definitely could have made a mistake somewhere since I typed it straight into discord

#

what's the error?

quaint nimbus
#

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

ocean seal
#

swearing is fine haha

#

no rules against that

quaint nimbus
#

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

ocean seal
#

What event is this code in? Is it in a different place from your previous instance_create_layer code?

quaint nimbus
#

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

ocean seal
#

gotcha, I assumed this code was on the player

#

right now the base x and y it's spawning at is that of objGame

quaint nimbus
#

ohhhhhhhhh

ocean seal
#

so you also need to change that to be based on the player object

quaint nimbus
#

okay, now they're spawning closer, but they're behind the player

ocean seal
#

hmm is your player's sprite backwards maybe?

#

wondering if image_angle is reverse of what you'd expect

quaint nimbus
#

that is... possible?

ocean seal
#

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

quaint nimbus
#

i did have some sprite and direction confusion at the start of the project so that's likely it

ocean seal
#

yeah in GM, a direction of 0 is facing right

#

so if your sprite isn't facing right, things will be offset weirdly

quaint nimbus
#

that explains so much actually

ocean seal
#

well you can choose between slapping in a +180, or rotating your sprite and removing your other workarounds haha

quaint nimbus
#

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?

ocean seal
#

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

quaint nimbus
#

that's it! exactly what i was hoping for :)