PDA

View Full Version : skycrane


Pillhead2007
20/03/2014, 01:56 AM
im trying to make a skycrane for helicopter I got most things right but I cant seem to get the closest car to attach to the object that's attached to my helicopter here is my code please help me new Float:VMX,Float:VMY,Float:VMZ;
new Magnet4Players[MAX_PLAYERS];
new MagnetTimer;
forward MagnetMovementUp(playerid);
forward MagnetMovementDown(playerid);
enum MInfo
{
Float:MagnetPos,
Magnet,
Magnetic,
IsMagnetActive,
};
new MagnetInfo[MAX_PLAYERS][MInfo];
stock GetClosestVehicle(playerid, Float:Range = 100.0)
{
new ClosestVehicle;
new Float:ClosestDist = 100000.0;
new Float:DistX;
new Float:DistY;
new Float:DistZ;
new Float:px;
new Float:py;
new Float:pz;
new Float:CheckDist;

// Get the players position
GetPlayerPos(playerid, px, py, pz);

// Loop through vehicles to find cloest vehicle
for(new i = 0; i < MAX_VEHICLES; i++)
{
// Vehicle doesn't exist
if(!GetVehicleModel(i)) continue;

// Get vehicle position
GetVehiclePos(i, DistX, DistY, DistZ);

// Get the distance
CheckDist = floatsqroot(
floatpower(floatsub(DistX, px), 2.0) +
floatpower(floatsub(DistY, py), 2.0) +
floatpower(floatsub(DistZ, pz), 2.0));

// Distance less than range?
if(CheckDist <= Range)
{
// Distance less than cloest distance, new cloest distance!
if(CheckDist < ClosestDist)
{
ClosestDist = CheckDist;
ClosestVehicle = i;
}
}
}
// Cloest less than or equal to the range return the closest vehicle ID
if(ClosestDist <= Range) return ClosestVehicle;
// no vehichle? return INVALID_VEHICLE_ID
return INVALID_VEHICLE_ID;
}
stock HelicopterMagnet(playerid, vehicleid)
{
new modelid = GetVehicleModel(vehicleid);
switch(modelid)
{
case 417:
{
GetVehiclePos(vehicleid,VMX,VMY,VMZ);
Magnet4Players[playerid] = CreateObject(3053, VMX, VMY, VMZ-0.0, 0.0, 0.0, 0.0, 96);
MagnetInfo[playerid][MagnetPos] = 0.0;
AttachObjectToVehicle(Magnet4Players[playerid], vehicleid, 0.0, 0.0, MagnetInfo[playerid][MagnetPos], 0.0, 0.0, 0.0);
MagnetInfo[playerid][Magnet] = 1;
MagnetInfo[playerid][Magnetic] = 1;
MagnetInfo[playerid][IsMagnetActive] = 0;
return 1;
}
}
return 1;
}
stock DestroyMagnet(playerid)
{
if(MagnetInfo[playerid][Magnetic] == 1)
{
MagnetInfo[playerid][Magnet] = 0;
DestroyObject[Magnet4Players[playerid]);
return 1;
}
return 1;
}
public MagnetMovementUp(playerid)
{
new vehicleid = GetPlayerVehicleID(playerid);
if(MagnetInfo[playerid][MagnetPos] <= -1.4)
{
GetObjectPos(Magnet4Players[playerid], VMX, VMY, VMZ);
MagnetInfo[playerid][MagnetPos] = MagnetInfo[playerid][MagnetPos]+0.1;
if(MagnetInfo[playerid][MagnetPos] >= -1.5)
{
MagnetInfo[playerid][MagnetPos] = -1.5;
}
MoveObject(Magnet4Players[playerid], VMX, VMY, MagnetInfo[playerid][MagnetPos],1.0);
AttachObjectToVehicle(Magnet4Players[playerid], vehicleid, 0.0, 0.0, MagnetInfo[playerid][MagnetPos], 0.0, 0.0, 0.0);
SetCameraBehindPlayer(playerid);
}
return 1;
}
public MagnetMovementDown(playerid)
{
new vehicleid = GetPlayerVehicleID(playerid);
if(MagnetInfo[playerid][MagnetPos] <= 0.0)
{
GetObjectPos(Magnet4Players[playerid], VMX, VMY, VMZ);
MagnetInfo[playerid][MagnetPos] = MagnetInfo[playerid][MagnetPos]-0.1;
MoveObject(Magnet4Players[playerid], VMX, VMY, MagnetInfo[playerid][MagnetPos],1.0);
AttachObjectToVehicle(Magnet4Players[playerid], vehicleid, 0.0, 0.0, MagnetInfo[playerid][MagnetPos], 0.0, 0.0, 0.0);
SetCameraBehindPlayer(playerid);
}
else if(MagnetInfo[playerid][MagnetPos] >= 1.5)
{
GetObjectPos(Magnet4Players[playerid], VMX, VMY, VMZ);
MagnetInfo[playerid][MagnetPos] = -1.5;
MoveObject(Magnet4Players[playerid], VMX, VMY, MagnetInfo[playerid][MagnetPos],1.0);
AttachObjectToVehicle(Magnet4Players[playerid], vehicleid, 0.0, 0.0, MagnetInfo[playerid][MagnetPos], 0.0, 0.0, 0.0);
}
return 1;
}public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
if(PRESSED(KEY_ANALOG_UP))
{
MagnetTimer = SetTimer("MagnetMovementUp",30,1);
return 1;
}
if(PRESSED(KEY_ANALOG_DOWN))
{
MagnetTimer = SetTimer("MagnetMovementDown",30,1);
return 1;
}
if(RELEASED(KEY_ANALOG_UP))
{
KillTimer(MagnetTimer);
}
if(RELEASED(KEY_ANALOG_DOWN))
{
KillTimer(MagnetTimer);
}
if(PRESSED(KEY_CROUCH))
{
if(MagnetInfo[playerid][Magnetic] == 1)
{
if(MagnetInfo[playerid][IsMagnetActive] == 0)
{
new veh = GetClosestVehicle(playerid, 100.0);
if(veh != INVALID_VEHICLE_ID)
{
MagnetInfo[playerid][IsMagnetActive] = 1;
GetObjectPos(Magnet4Players[playerid], VMX, VMY, MagnetInfo[playerid][MagnetPos]);
AttachTrailerToVehicle(veh, Magnet4Players[playerid]);
//AttachTrailerToVehicle(Magnet4Players[playerid],veh,VMX,VMY, MagnetInfo[playerid][MagnetPos],0.0,0.0,0.0);
}
}
else
{
MagnetInfo[playerid][IsMagnetActive] = 0;
}
}
}
return 1;
}
public OnPlayerStateChange(playerid, newstate, oldstate)
{
if(newstate == 2 && oldstate == 1)
{
new vehicleid = GetPlayerVehicleID(playerid);
HelicopterMagnet(playerid,vehicleid);
}
return 1;
}

Pillhead2007
21/03/2014, 01:04 PM
anyone still need help with this ive made about 5 filterscripts different ways with other peoples distance native still no joy any1 skilled enough ??? starting do me heading I don't want no timers it will lag too much want it smooth as f*CK please please help wrecking my head hahaha:'(

Carac
21/03/2014, 02:48 PM
Don't try to get stressed and keep tight mate, your answer will probably come soon enough.

Pillhead2007
05/07/2014, 04:08 AM
still hasn't came lol

Dignity
05/07/2014, 04:32 AM
The code seems to be okay, you just need to experiment with things. Did you try playing around with the range?

stock GetClosestVehicle(playerid, Float:Range = 100.0)