How to make object have Health
#1

Hey , Guy I want to make object have health you can help me ?

I Want to set object have Health 10,000 HP How to make that
Reply
#2

Help please
Reply
#3

how to make object with health
Reply
#4

There is very limited support for this which you basically have to create yourself, there is no collision detection either between the player shooting and the object (At least that is accessible). Anyways what you need to do is see if a line starting at an origin (x,y,z) and ending at a destination (x,y,z) intersects a bounding box which can either be a sphere or a box. There is obviously different code for a sphere or box fortunately you I have researched and developed this concept.

Please keep in mind this is not a perfect solution but can give you pretty good results, the functions provided require code to actually use them, the object you wish to damage requires to be coded this is only a method to detect when it gets damaged. This can also work for detecting damage on a NPC, Vehicles or even other players which I wouldn't never recommend using.

Server sided vehicle damage FS based on this
https://sampforum.blast.hk/showthread.php?pid=2695108#pid2695108

Basically this provides you with two functions and one callback I will explain their parameters.

OnPlayerShoot(playerid,weaponid,ammo)
This is called when the script detects a player firing their weapon and is usually when you would call
BulletCollisionSphere() or BulletCollisionBox() it is recommend that you use IsPlayerInRangeOfPoint() to make sure the player is in range of the object which you are trying to detect damage for otherwise BulletCollisionSphere() and BulletCollisionBox() will be called more often than needed.


Код:
BulletCollisionSphere()
Parameters (playerid, weapon, Float:sx, Float:sy, Float:sz, Float:fScale = 30.0, Float:radius = 1.0)
playerid - id of player to check
weapon - weapon (only used to check for sniper)
Float:sx - Positon of sphere X
Float:sy - Positon of sphere Y
Float:sz - Positon of sphere Z
Float:fScale = 30.0 - (Optional) Scales the vector (Higher values detect from further away)
Float:radius - (Optional) Radiuss of the sphere
Returns true if a player is aiming at and intersects a sphere


Код:
BulletCollisionBox() - Returns true if a player is aiming at and intersects a box
Parameters (playerid, weapon, Float:b1x, Float:b1y, Float:b1z, Float:b2x, Float:b2y, Float:b2z, Float:fScale = 30.0)
playerid - id of player to check
weapon - weapon (only used to check for sniper)
b1x - Bound box min X
b1y - Bound box min Y
b1z - Bound box min Z
b1x - Bound box max X
b1y - Bound box max Y
b1z - Bound box max Z
Float:fScale = 30.0 - (Optional) Scales the vector (Higher values detect from further away)
pawn Код:
new OldAmmo[MAX_PLAYERS], OldWeap[MAX_PLAYERS];
new CurrAmmo[MAX_PLAYERS], CurrWeap[MAX_PLAYERS];

// Used for detecting when a player shoots
// Note: - hackers will locked ammo will not be able to trigger this
//       - You must hook this callback yourself

forward OnPlayerShoot(playerid,weaponid,ammo);

public OnPlayerUpdate(playerid)
{
    // Get the current weapon and ammo
    CurrWeap[playerid] = GetPlayerWeapon(playerid);
    CurrAmmo[playerid] = GetPlayerAmmo(playerid);

    // Player still has old weapon does this weapon now have less ammo?
    if(CurrWeap[playerid] == OldWeap[playerid] && CurrAmmo[playerid] < OldAmmo[playerid])
    {
        CallLocalFunction( "OnPlayerShoot", "iii", playerid, CurrWeap[playerid], CurrAmmo[playerid]);
    }
    OldWeap[playerid] = CurrWeap[playerid];
    OldAmmo[playerid] = CurrAmmo[playerid];
    return 1;
}

// Checks if a players bullet intersects a sphere
stock BulletCollisionSphere(playerid, weapon, Float:sx, Float:sy, Float:sz, Float:fScale = 30.0, Float:radius = 1.0)
{
    new
        Float:fP[3],
        Float:fV[3],
        Float:object[3],
        Float:sphere[3];

    sphere[0] = sx, sphere[1] = sy, sphere[2] = sz;

    GetPlayerCameraPos(playerid, fP[0], fP[1], fP[2]);
    GetPlayerCameraFrontVector(playerid, fV[0], fV[1], fV[2]);

    // Compensate (This is not perfect yet any ideas anyone?)
    if(weapon != 34)
    {
        new Float:FacingA;
        GetPlayerFacingAngle(playerid, FacingA);
        FacingA -= 90.0;
        if(FacingA < 0.0) FacingA += 360.0;
        else if(FacingA > 360.0) FacingA -= 360.0;

        fP[0] = (fP[0] + 0.6 * floatsin(-FacingA,degrees));
        fP[2] += 1.2;
    }

    object[0] = fP[0] + floatmul(fV[0], fScale);
    object[1] = fP[1] + floatmul(fV[1], fScale);
    object[2] = fP[2] + floatmul(fV[2], fScale);

    // Check if line intersects sphere
    if(RaySphere(fP, object, sphere, radius)) return 1;
    return 0;
}

// Checks if a players bullet intersects a bounding box
stock BulletCollisionBox(playerid, weapon, Float:b1x, Float:b1y, Float:b1z, Float:b2x, Float:b2y, Float:b2z, Float:fScale = 30.0)
{
    new
        Float:fP[3],
        Float:fV[3],
        Float:object[3],
        Float:sphere[3];

    new Float:b1[3];
    new Float:b2[3];

    b1[0] = b1x;
    b1[1] = b1y;
    b1[2] = b1z;

    b2[0] = b2x;
    b2[1] = b2y;
    b2[2] = b2z;

    GetPlayerCameraPos(playerid, fP[0], fP[1], fP[2]);
    GetPlayerCameraFrontVector(playerid, fV[0], fV[1], fV[2]);

    // Compensate (This is not perfect yet any ideas anyone?)
    if(weapon != 34)
    {
        new Float:FacingA;
        GetPlayerFacingAngle(playerid, FacingA);
        FacingA -= 90.0;
        if(FacingA < 0.0) FacingA += 360.0;
        else if(FacingA > 360.0) FacingA -= 360.0;

        fP[0] = (fP[0] + 0.6 * floatsin(-FacingA,degrees));
        fP[2] += 1.2;
    }

    object[0] = fP[0] + floatmul(fV[0], fScale);
    object[1] = fP[1] + floatmul(fV[1], fScale);
    object[2] = fP[2] + floatmul(fV[2], fScale);

    // Check if line intersects box
    if(CheckLineBox( b1, b2, fP, object)) return 1;
    return 0;
}


stock RaySphere(Float:p1[3],Float:p2[3],Float:sc[3],Float:r)
{
   new Float:a, Float:b, Float:c;
   new Float:bb4ac;
   new Float:dp[3];

   dp[0] = p2[0] - p1[0];
   dp[1] = p2[1] - p1[1];
   dp[2] = p2[2] - p1[2];
   a = dp[0] * dp[0] + dp[1] * dp[1] + dp[2] * dp[2];
   b = 2 * (dp[0] * (p1[0] - sc[0]) + dp[1] * (p1[1] - sc[1]) + dp[2] * (p1[2] - sc[2]));
   c = sc[0] * sc[0] + sc[1] * sc[1] + sc[2] * sc[2];
   c += p1[0] * p1[0] + p1[1] * p1[1] + p1[2] * p1[2];
   c -= 2 * (sc[0] * p1[0] + sc[1] * p1[1] + sc[2] * p1[2]);
   c -= r * r;
   bb4ac = b * b - 4 * a * c;
   if(bb4ac < 0) return 0;
   return 1;
}


// returns true if line (L1, L2) intersects with the box (B1, B2)
// returns intersection point in Hit
stock CheckLineBox( Float:B1[3], Float:B2[3], Float:L1[3], Float:L2[3])
{
    if (L2[0] < B1[0] && L1[0] < B1[0]) return false;
    if (L2[0] > B2[0] && L1[0] > B2[0]) return false;
    if (L2[1] < B1[1] && L1[1] < B1[1]) return false;
    if (L2[1] > B2[1] && L1[1] > B2[1]) return false;
    if (L2[2] < B1[2] && L1[2] < B1[2]) return false;
    if (L2[2] > B2[2] && L1[2] > B2[2]) return false;
    if (L1[0] > B1[0] && L1[0] < B2[0] &&
        L1[1] > B1[1] && L1[1] < B2[1] &&
        L1[2] > B1[2] && L1[2] < B2[2]) return true;

    new Float:Hit[3];

    if ( (GetBoxIntersection( L1[0]-B1[0], L2[0]-B1[0], L1, L2, Hit) && InBox( Hit, B1, B2, 1 ))
      || (GetBoxIntersection( L1[1]-B1[1], L2[1]-B1[1], L1, L2, Hit) && InBox( Hit, B1, B2, 2 ))
      || (GetBoxIntersection( L1[2]-B1[2], L2[2]-B1[2], L1, L2, Hit) && InBox( Hit, B1, B2, 3 ))
      || (GetBoxIntersection( L1[0]-B2[0], L2[0]-B2[0], L1, L2, Hit) && InBox( Hit, B1, B2, 1 ))
      || (GetBoxIntersection( L1[1]-B2[1], L2[1]-B2[1], L1, L2, Hit) && InBox( Hit, B1, B2, 2 ))
      || (GetBoxIntersection( L1[2]-B2[2], L2[2]-B2[2], L1, L2, Hit) && InBox( Hit, B1, B2, 3 ))) return true;

    return false;
}

stock GetBoxIntersection(Float:fDst1, Float:fDst2, Float:P1[3], Float:P2[3], Float:hit[3])
{
    if ( (fDst1 * fDst2) >= 0.0) return 0;
    if ( fDst1 == fDst2) return 0;
    new Float:fdst = -fDst1/(fDst2-fDst1);

    hit[0] = P1[0] + (P2[0]-P1[0]) * (fdst);
    hit[1] = P1[1] + (P2[1]-P1[1]) * (fdst);
    hit[2] = P1[2] + (P2[2]-P1[2]) * (fdst);

    return 1;
}

stock InBox( Float:Hit[3], Float:B1[3], Float:B2[3], Axis)
{
    if ( Axis==1 && Hit[2] > B1[2] && Hit[2] < B2[2] && Hit[1] > B1[1] && Hit[1] < B2[1]) return 1;
    if ( Axis==2 && Hit[2] > B1[2] && Hit[2] < B2[2] && Hit[0] > B1[0] && Hit[0] < B2[0]) return 1;
    if ( Axis==3 && Hit[0] > B1[0] && Hit[0] < B2[0] && Hit[1] > B1[1] && Hit[1] < B2[1]) return 1;
    return 0;
}
Reply
#5

I will try it. / Thanks. you
Reply
#6

I forgot one thing check my post for the update thanks.
Reply
#7

You can simple script ?
Reply
#8

Check the sever sided vehicle damage it will show you how it works but it's really up to you to use this method if you can't figure it out from here try doing some easier scripts first.
Reply
#9

Ok
Reply
#10

I can't to make object with health
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)