[Tutorial] Simple vehicle and pickup spawning system
#1

okay, this tutorial is to teach people how to use the polar coordinates sytem to find where to spawn a vehicle or pickup if you want it directly in front of the player, but i will include some commands to start you off

lets begin with a math lesson. the polar coordinates system is a coordinates system based on a circle, where the center is the origin, 0 degrees is on the right and it progresses anti-clockwise. the coordinates are both the angle between the point around the circle and the radius of the circle, so 5,90 would be a circle with a radius of five units, and the angle is 90 degrees. so if we take the angle the player is facing, and the amount of units in front of the player we want the vehicle to spawn, we have our polar coordinates. we can convert this to cartesian coordinates (aka, xy coordinates) with these forumulas

Code:
x = r * cos(O)
y = r * sin(O)
(in this example r is the distance in front of the player you want the vehicle to spawn, and O is the players facing angle plus 90 because 0 starts at the top of the circle in samp)

so im just going to punch in some code and explain what you do with it, if you understand it you can go ahead and hack it up, but if you dont, please dont touch anything but the spawn distance


Code:
     new Float:final_x,Float:final_y,Float:final_z,Float:rotation,spawn_distance,Float:addon_x,Float:addon_y;
     
         spawn_distance = 10;
     
	 GetPlayerPos(playerid,final_x,final_y,final_z);
	 GetPlayerFacingAngle(playerid,rotation);
	 addon_x = spawn_distance*floatcos(rotation+90,degrees);
	 addon_y = spawn_distance*floatsin(rotation+90,degrees);
	 final_x += addon_x;
	 final_y += addon_y;
then you can use the variables final_x, final_y and final_z to spawn your vehicle, like this

Code:
public OnPlayerCommandText(playerid, cmdtext[])
{

     new Float:final_x,Float:final_y,Float:final_z,Float:rotation,spawn_distance,Float:addon_x,Float:addon_y;
     
     spawn_distance = 10;
     
	 GetPlayerPos(playerid,final_x,final_y,final_z);
	 GetPlayerFacingAngle(playerid,rotation);
	 addon_x = spawn_distance*floatcos(rotation+90,degrees);
	 addon_y = spawn_distance*floatsin(rotation+90,degrees);
	 final_x += addon_x;
	 final_y += addon_y;
	

     if(strcmp(cmdtext, "/liftoff", true) == 0)
     {
          CreateVehicle(520,final_x,final_y,final_z,rotation,-1,-1,-1);
          return 1;
     }
     return 0;
}
this will spawn a hydra, 10 units in front of the player, when they type /liftoff

have fun, and as i said in my last tutorial, if you have any ideas about what i could do a tutorial on, please say so, i need more ideas lol
Reply
#2

Could you tell me, how to set a vehicle's max speed?
Reply
#3

well, you would have to chesck a vehicles speed, either with the OnPlayerUpdate or with a timer, and see if its over a cirtain speed, and if it is, set it to a lower speed
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)