Spawn random around the player
#1

Hello people,

I was wondering how do i spawn multiple objects with randomly spots but around the player with some distance?
Reply
#2

This could be accomplished by offsetting the players position with sin and cos variables (with changing the angle too). That way you're not just restricted to the x and y axis.

pawn Code:
new Float: x, Float: y, Float: z, Float: a;
GetPlayerPos( playerid, x, y, z );
GetPlayerFacingAngle( playerid, a );
a += random( 360 );
x += ( random( 10 ) * floatsin( -a, degrees ) );
y += ( random( 10 ) * floatcos( -a, degrees ) );
CreateObject( 0, x, y, z, 0.00, 0.00, 0.00 );
Notes:
- The random( 360) will add a random value to the angle, so it spawns at a different direction from the player
- The random( 10 ) will add a random value from the player, so they'll be at different distances, not always 5, or whatever
- If you would like a constant distance from the player, exchange random( 10 ) with the number of units you desire
- You could place this code under some type of timer
Reply
#3

Quote:
Originally Posted by Bakr
View Post
This could be accomplished by offsetting the players position with sin and cos variables (with changing the angle too). That way you're not just restricted to the x and y axis.

pawn Code:
new Float: x, Float: y, Float: z, Float: a;
GetPlayerPos( playerid, x, y, z );
GetPlayerFacingAngle( playerid, a );
a += random( 360 );
x += ( random( 10 ) * floatsin( -a, degrees ) );
y += ( random( 10 ) * floatcos( -a, degrees ) );
CreateObject( 0, x, y, z, 0.00, 0.00, 0.00 );
Notes:
- The random( 360) will add a random value to the angle, so it spawns at a different direction from the player
- The random( 10 ) will add a random value from the player, so they'll be at different distances, not always 5, or whatever
- If you would like a constant distance from the player, exchange random( 10 ) with the number of units you desire
- You could place this code under some type of timer
Awsome thanks, this should work.
But i have 1 more question, i have 2 teams (lets say, TEAM_A and TEAM_B)
now i want the objects only spawn RANDOM players in team A, so i could use random(MAX_PLAYERS) but thats for all and i need to make it select the players in team_a.
Reply
#4

then do something like this
pawn Code:
new Float: x[MAX_PLAYERS], Float: y[MAX_PLAYERS], Float: z[MAX_PLAYERS], Float: a[MAX_PLAYERS];
//so it would be for a player each

//then
if(TEAM_A){//blablabla
GetPlayerPos( playerid, x[playerid], y[playerid], z[playerid]);
GetPlayerFacingAngle( playerid, a[playerid]);
a[playerid] += random( 360 );
x[playerid] += ( random( 10 ) * floatsin( -a[playerid], degrees ) );
y[playerid] += ( random( 10 ) * floatcos( -a[playerid], degrees ) );
CreateObject( 0, x[playerid], y[playerid], z[playerid], 0.00, 0.00, 0.00 );
}
Reply
#5

Quote:
Originally Posted by park4bmx
View Post
then do something like this
pawn Code:
new Float: x[MAX_PLAYERS], Float: y[MAX_PLAYERS], Float: z[MAX_PLAYERS], Float: a[MAX_PLAYERS];
//so it would be for a player each

//then
if(TEAM_A){//blablabla
GetPlayerPos( playerid, x[playerid], y[playerid], z[playerid]);
GetPlayerFacingAngle( playerid, a[playerid]);
a[playerid] += random( 360 );
x[playerid] += ( random( 10 ) * floatsin( -a[playerid], degrees ) );
y[playerid] += ( random( 10 ) * floatcos( -a[playerid], degrees ) );
CreateObject( 0, x[playerid], y[playerid], z[playerid], 0.00, 0.00, 0.00 );
}
no that doesnt work.

I need a code that randomly gets a player id at that team, and the player needs to be alive
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)