system doesn't need to consider vehicle relative positsion and other useless things, when system finds nearest vehicle then just put player into interior and everything is working like it needs to be.
PHP Code:
//vehicle ids start from 1
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys){
if(newkeys & KEY_CTRL_BACK) {
if(PlayerInfo[playerid][pInVehicle] != 0 || !IsPlayerInAnyVehicle(playerid))return 1;
//finding nearest vehicle
new nearestVeh=getNearestVehicle(playerid,5.0),
if(!nearestVeh)return SendClientMessage(playerid, COLOR_ORANGE, "you aren't near to any vehicle!");
if(VehicleInfo[nearestVeh][vLocked])return SendClientMessage(playerid, COLOR_ORANGE, "vehicle is locked!");
//finding nearest vehicle model
new vehInt=getVehIntFromModel(GetVehicleModel(nearestVeh));
if(vehInt==VEHICLE_INTERIOR_INVALID)return SendClientMessage(playerid, COLOR_ORANGE, "not enterable vehicle!");
//putting player into interior
GetPlayerPos(playerid, PlayerInfo[playerid][EnterVehPos][0], PlayerInfo[playerid][EnterVehPos][1], PlayerInfo[playerid][EnterVehPos][2]);
SetPlayerPos(playerid, VehicleInteriorPos[vehInt][vIntX], VehicleInteriorPos[vehInt][vIntY], VehicleInteriorPos[vehInt][vIntZ]);
SetPlayerVirtualWorld(playerid, nearestVeh);
SetPlayerInterior(playerid, nearestVeh);
PlayerInfo[playerid][pInVehicle] = nearestVeh;
SendClientMessage(playerid, COLOR_WHITE, "Use '/exit' or click the door hotkey to leave this vehicle.");
TempFreeze(playerid);
}
return 1;
}
//vehicle related functions
getNearestVehicle(playerid,Float:radius){
new lastVeh=0,Float:x, Float:y, Float:z;
GetPlayerPos(playerid,x,y,z);
foreach(new i : Vehicle){
if( (dis=GetVehicleDistanceFromPoint(playerid, x, y, z)) <= radius ){
radius=dis;
lastVeh=i;
}
}
return lastVeh;
}
getVehIntFromModel(model){
if(model == 508)return VEHICLE_INTERIOR_JOURNEY;
if(model == 427)return VEHICLE_INTERIOR_ENFORCER;
if(model == 416)return VEHICLE_INTERIOR_AMBULANCE;
if(model == 577)return VEHICLE_INTERIOR_AT400;
if(model == 454)return VEHICLE_INTERIOR_TROPIC;
return VEHICLE_INTERIOR_INVALID;
}
exitFromVehInt(pid){
if(!PlayerInfo[pid][pInVehicle])return 1;
SetPlayerPos(pid, PlayerInfo[pid][EnterVehPos][0], PlayerInfo[pid][EnterVehPos][1], PlayerInfo[pid][EnterVehPos][2]);
SetPlayerVirtualWorld(pid, 0);
SetPlayerInterior(pid, 0);
PlayerInfo[pid][pInVehicle]=0;
return 1;
}
//player related functions
tempFreeze(pid){
TogglePlayerControllable(playerid,false);
return SetTimerEx("tempUnfreeze", 1500, false, "i", playerid);
}
forward tempUnfreeze(playerid);
public tempUnfreeze(playerid){
return TogglePlayerControllable(playerid,true);
}