Creating enemy away from player

Hello !

I’m trying to create enemies at random locations but I want them to appear out of the screen so we don’t see an enemy appearing randomly in front of the player.

I tried something with : Create Enemy at position Random(1500);Random(1500) but of course it can appear near the player.
So I was trying to find something like Player-Random(1500) but I don’t know the correct way to spell it.

Does anyone have a solution ?

Thanks in advance ! :smiley:

Hi,

Here is my idea : try to create the enemy at a specific distance from the player.

So, there are two random variables here : the distance (for example, random between 1000 and 1200) and the angle (random between 0 and 360). So, you need to affect a random value to both of these variables before creating the enemy.
Then, use these two variables to set the X and Y position of the newly created enemy :

X = Variable(distance) * cos(Variable(angle)) Y = Variable(distance) * sin(Variable(angle))
This assumes that the player is at 0;0. If it’s not the case, just add respectively Player.X() and Player.Y() to the previous formulas.

The player won’t always be at 0;0 as he is moving.

So the right way would be PlayerX(VARIABLE1);PlayerY(VARIABLE1) ?

To create enemies at a random distance from the player:

Create Enemy at position Player.X() + Random(1500); Player.Y() + Random(1500)

This however, can create enemies at any distance from the player, including on top of it! To put some kind of minimum distance add this.

Create Enemy at position Player.X() + Variable(mindistance) + Random(variation); Player.Y() + Variable(mindistance) + Random(variation)

Where mindistance is a scene variable you set as the minimum distance away you want something to be rom the player and the variable variation is how much variation in distance you want for the enemies.

In this case, you will only make enemies in a region +X +Y from the player. If you want to make enemies in any position around the player, then you need to add one final thing:

[code]
Do = Random(2) to variable(xsign)
Do = Random(2) to variable(ysign)

Variable xsign = 0;
Variable ysign = 0;
Create Enemy at position Player.X() + Variable(mindistance) + Random(variation); Player.Y() + Variable(mindistance) + Random(variation)

Variable xsign = 1;
Variable ysign = 0;
Create Enemy at position Player.X() - Variable(mindistance) - Random(variation); Player.Y() + Variable(mindistance) + Random(variation)
etc…[/code]

And then you will have enemies spawning randomly all around the player.

This isn’t the only method of making enemies spawn around the player, but it works without any trigonometry.

It would be

X = Variable(distance) * cos(Variable(angle)) + Player.X() Y = Variable(distance) * sin(Variable(angle)) + Player.Y()

Excuse the intrusion, just to clarify a bit :slight_smile:

Mats suggestion gives you random zones in rectangles. A random rectangle is defined by a position plus random size, and returns a rectangle without holes. You need a void zone around the player (the minimum distance), so you need an hole, that’s why Mats needs four rectangular zones.
By the way, the “distance” is not the common distance, it’s the distance in width or height of the rectangles.

Victor suggestion is a random zone in polar coordinates, i.e. an angle and distance from a position. The final zone is a circle (or cone) with or without a smaller circle as void zone inside. It uses the common definition of distance (euclidian distance, the distance in a straight line between A and B), and you can easily set an hole with the distance variable as Victor shows. You can set “cones” with the angle variable too :wink:

Hm thanks you all for the help but I’m kinad confused with everyone saying different things.
And all this looks quite complicated so I don’t really get it.
What is the code I should have at the end ?
Sorry again and thank you for helping !