You need to do Object Pos - Vehicle Pos (x, y and z seperately) - that's correct. But for that to work the vehicle must face north. If you attach an object the coords you need are relative to the vehicle's axis. What you calculated is relative to the World.
If you have a custom mod garage or something, the easiest way would be to make the vehicle face north.
If the vehicle is rotated on the Z axis, you can get the vehicle Z angle and rotate the object coords by the opposite of that (using sin and cos), then rotate the object as well so that the Z rotation fits the new position.
Код:
GetObjectAttachmentOffset(objectid, vehicleid, &Float:att_x, &Float:att_y, &Float:att_z, &Float:att_rx, &Float:att_ry, &Float:att_rz)
{
new Float:v_x, Float:v_y, Float:v_z, Float:v_rz,
Float:o_x, Float:o_y, Float:o_z, Float:o_rx, Float:o_ry, Float:o_rz;
GetVehiclePos(vehicleid, v_x, v_y, v_z);
GetVehicleZAngle(vehicleid, v_rz);
GetObjectPos(objectid, o_x, o_y, o_z);
GetObjectRot(objectid, o_rx, o_ry, o_rz);
new Float:o_distance = VectorSize(v_x - o_x, v_y - o_y, 0.0), // Calculate the X/Y distance from the vehicle to the object so we can place it again in the new direction
Float:o_angle = (atan2(o_y - v_y, o_x - v_x) - 90.0) - v_rz; // Get the object's angle relative to the vehicle's angle
o_x = v_x + o_distance * floatsin(-o_angle, degrees); // Rotate the object position by the difference calculated above (o_angle)
o_y = v_y + o_distance * floatcos(-o_angle, degrees);
o_rz -= v_rz; // Adjust the Z angle of the object to face the original direction (rotate it by the opposite of the vehicle's angle)
att_x = o_x - v_x;
att_y = o_y - v_y;
att_z = o_z - v_z;
att_rx = o_rx;
att_ry = o_ry;
att_rz = o_rz;
return 1;
}
This should do it.
It calculates the offset of an object to the vehicle relative to the vehicle's rotation.
So if you create an object on the vehicle (no matter the vehicle's Z rotation) it will give you the attachment offsets for exactly that position.
Make sure the vehicle is on a flat surface otherwise the position will be a bit off.