Getting position behind vehicle -
TEKNOJIHAD1 - 20.06.2017
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(PlayerKartID, VehicleX, VehicleY, VehicleZ);
CreateObject(1225, VehicleX, VehicleY, VehicleZ+1, 0.0, 0.0, 125.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
Re: Getting position behind vehicle -
Vince - 20.06.2017
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(1225, VehicleX + DISTANCE * floatsin(-angle, degrees), VehicleY + DISTANCE * floatcos(-angle, degrees), vehicleZ + 1.0, 0.0, 0.0, angle);
Re: Getting position behind vehicle -
TEKNOJIHAD1 - 20.06.2017
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(PlayerKartID, VehicleX, VehicleY, VehicleZ);
new const Float:DISTANCE = 2.0;
new Float:angle;
GetVehicleZAngle(PlayerKartID,angle);
angle = angle-180.0;
new Float:F_FLOATSIN = floatsin(-angle, degrees);
new Float:F_FLOATCOS = floatcos(-angle, degrees);
CreateObject(1225, VehicleX + DISTANCE * F_FLOATSIN, VehicleY + DISTANCE * F_FLOATCOS, VehicleZ, 0.0, 0.0, 125.0);
Thanks alot
Re: Getting position behind vehicle -
Eoussama - 20.06.2017
Here's the function I use
DISCLAIMER: I didn't make the function,
PHP код:
stock GetPosBehindVehicle(vehicleid, &Float:x, &Float:y, &Float:z, Float:offset=0.5) //Credits go to MP2
{
new Float:vehicleSize[3], Float:vehiclePos[3];
GetVehiclePos(vehicleid, vehiclePos[0], vehiclePos[1], vehiclePos[2]);
GetVehicleModelInfo(GetVehicleModel(vehicleid), VEHICLE_MODEL_INFO_SIZE, vehicleSize[0], vehicleSize[1], vehicleSize[2]);
GetXYBehindVehicle(vehicleid, vehiclePos[0], vehiclePos[1], (vehicleSize[1]/2)+offset);
x = vehiclePos[0];
y = vehiclePos[1];
z = vehiclePos[2];
return 1;
}
stock GetXYBehindVehicle(vehicleid, &Float:q, &Float:w, Float:distance)//Credits go to MP2
{
new Float:a;
GetVehiclePos(vehicleid, q, w, a);
GetVehicleZAngle(vehicleid, a);
q += (distance * -floatsin(-a, degrees));
w += (distance * -floatcos(-a, degrees));
}