Tracking Car Missiles ?
#1

hi guys i wanted to make tracking car missiles so specific cars have got missiles attached to them and when i hit a specific button the missile dispatches and seek the closest car in range of 50 units in 3d and the missile explodes in 15 seconds after . i got no problems with the objects attached to the vehicle but i just dunno how to make the missile rotates to look at the target and the track him and when it is in range of 5 units from him it explodes
Reply
#2

You would need to calculate the angle from the missile to the player, then rotate it to face the player.

pawn Код:
stock Float:GetAngleFromPosToPos(Float:X, Float:Y, Float:PointX, Float:PointY)
{
    new Float:Angle;
    if(X > PointX && Y > PointY)
        Angle = floatabs(atan2(floatsub(PointX, X), floatsub(PointY, Y)));
    if(X > PointX && Y <= PointY)
        Angle = floatadd(floatabs(atan2(floatsub(Y, PointY), floatsub(PointX, X))), 270.0);
    if(X <= PointX && Y > PointY)
        Angle = floatadd(floatabs(atan2(floatsub(PointY, Y), floatsub(X, PointX))), 90.0);
    if(X <= PointX && Y <= PointY)
        Angle = floatadd(floatabs(atan2(floatsub(X, PointX), floatsub(Y, PointY))), 180.0);

    return Angle >= 360.0 ? floatsub(Angle, 360.0) : Angle;
}
This function may not give the correct angle for the missile, you might (probably) need to test it and use rotation offsets.

EDIT: This function is only 2D angle, it will not make the missile point uphill for example.
Reply
#3

no problem if it is 2d okay thanx so now i have to set the object rot and then move the missile to x,y,z ?i think it won't track him it will go to his position when i hit the button
Reply
#4

A missile update function would look similar to this:

pawn Код:
//make sure this is defined before it is used, or get a reparse error
stock Float:GetAngleFromPosToPos(Float:X, Float:Y, Float:PointX, Float:PointY)
{
    new Float:Angle;
    if(X > PointX && Y > PointY)
        Angle = floatabs(atan2(floatsub(PointX, X), floatsub(PointY, Y)));
    if(X > PointX && Y <= PointY)
        Angle = floatadd(floatabs(atan2(floatsub(Y, PointY), floatsub(PointX, X))), 270.0);
    if(X <= PointX && Y > PointY)
        Angle = floatadd(floatabs(atan2(floatsub(PointY, Y), floatsub(X, PointX))), 90.0);
    if(X <= PointX && Y <= PointY)
        Angle = floatadd(floatabs(atan2(floatsub(X, PointX), floatsub(Y, PointY))), 180.0);

    return Angle >= 360.0 ? floatsub(Angle, 360.0) : Angle;
}

//This function makes objectid point to vehicle and move towards it
stock UpdateMissile(objectid, targetvehicle)
{
    new
        Float: fFromX,
        Float: fFromY,
        Float: fFromZ,
        Float: fToX,
        Float: fToY,
        Float: fToZ
    ;

    //save pos of missile and target so we can do angle calculations
    GetObjectPos(objectid, fFromX, fFromY, fFromZ);
    GetVehiclePos(targetvehicle, fToX, fToY, fToZ);

    //get angle to target
    new Float: fTargetAngle = GetAngleFromPosToPos(fFromX, fFromY, fToX, fToY);

    //This is where you need to get offsets
    //these are offsets i made for the arrow object (works perfect with the white arrow object)
    SetObjectRot(objectid, 0.0, -90.00000, 90.0+fTargetAngle);

    //use your own speeds for the missile
    MoveObject(objectid, fToX, fToY, fToZ, 1.0);
}
Keep in mind the offsets used here are just for the white arrow, wont necessarily work for the missile.

Just wrote that quickly and not tested it, it's more so you can see the logic.

EDIT: Fixed some code
Reply
#5

Thank u so much man now i get it
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)