Getting position behind vehicle
#1

Hi guys, I know there are lots of threads about getting the position behind a vehicle but I just can't understand them and I wanna use them in OnPlayerKeyStateChange

Here is the code

PHP код:
new Float:VehicleX;
new 
Float:VehicleY;
new 
Float:VehicleZ;
new 
PlayerKartID GetPlayerVehicleID(playerid);
GetVehiclePos(PlayerKartIDVehicleXVehicleYVehicleZ);
CreateObject(1225VehicleXVehicleYVehicleZ+10.00.0125.0); 
Right now it spawns the barrel above the player's kart which sometimes makes it explode. I want it to spawn right behind the kart like -2 xy or something but I just can't figure out the formula.

Thanks in advance
Reply
#2

There are plenty of implementations of GetXYInFrontOfPlayer that you can use. The only thing you need to do is change what exactly "the front" is. If you want something to the left you add 90 degrees to the facing angle, if you want something to the right you subtract 90 degrees from the facing angle and if you want something to the back then you either add or remove 180 to/from the facing angle (a circle being 360 degrees). GTA SA angles go counter clockwise for some odd reason which is why the angles are reversed.

PHP код:
new const Float:DISTANCE 2.0;
new 
Float:angle GetVehicleZAngle(PlayerKartID) - 180.0;
CreateObject(1225VehicleX DISTANCE floatsin(-angledegrees), VehicleY DISTANCE floatcos(-angledegrees), vehicleZ 1.00.00.0angle); 
Reply
#3

It works perfectly but I had to modify it a little to fix some warnings here is the code in case anyone needs it:

PHP код:
new Float:VehicleX;
new 
Float:VehicleY;
new 
Float:VehicleZ;
new 
PlayerKartID GetPlayerVehicleID(playerid);
GetVehiclePos(PlayerKartIDVehicleXVehicleYVehicleZ);
new const 
Float:DISTANCE 2.0;
new 
Float:angle;
GetVehicleZAngle(PlayerKartID,angle);
angle angle-180.0;
new 
Float:F_FLOATSIN floatsin(-angledegrees);
new 
Float:F_FLOATCOS floatcos(-angledegrees);
CreateObject(1225VehicleX DISTANCE F_FLOATSINVehicleY DISTANCE F_FLOATCOSVehicleZ0.00.0125.0); 
Thanks alot
Reply
#4

Here's the function I use
DISCLAIMER: I didn't make the function,
PHP код:

stock GetPosBehindVehicle
(vehicleid, &Float:x, &Float:y, &Float:zFloat:offset=0.5//Credits go to MP2
{
    new 
Float:vehicleSize[3], Float:vehiclePos[3];
    
GetVehiclePos(vehicleidvehiclePos[0], vehiclePos[1], vehiclePos[2]);
    
GetVehicleModelInfo(GetVehicleModel(vehicleid), VEHICLE_MODEL_INFO_SIZEvehicleSize[0], vehicleSize[1], vehicleSize[2]);
    
GetXYBehindVehicle(vehicleidvehiclePos[0], vehiclePos[1], (vehicleSize[1]/2)+offset);
    
vehiclePos[0];
    
vehiclePos[1];
    
vehiclePos[2];
    return 
1;
}
stock GetXYBehindVehicle(vehicleid, &Float:q, &Float:wFloat:distance)//Credits go to MP2
{
    new 
Float:a;
    
GetVehiclePos(vehicleidqwa);
    
GetVehicleZAngle(vehicleida);
    
+= (distance * -floatsin(-adegrees));
    
+= (distance * -floatcos(-adegrees));

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)