SA-MP Forums Archive
Detect if player is hitting a door - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Detect if player is hitting a door (/showthread.php?tid=602821)



Detect if player is hitting a door - AidanRO - 13.03.2016

Hello, today I want some help from you guys.

I recently wanted to remake the way you break in a car on my server. So, now I want to know how I can detect if a player is hitting any of the doors with his fist or baseball bat, or anything.

Is there any way to do that?
Thanks.


Re: Detect if player is hitting a door - AndySedeyn - 13.03.2016

Here's a snippet to detect when a player is NOT at any of the doors but rather at the trunk or hood. Credits go to Emmet_:

PHP код:
IsPlayerNearBoot(playeridvehicleid) {
    static
        
Float:fX,
        
Float:fY,
        
Float:fZ;
    
GetVehicleBoot(vehicleidfXfYfZ);
    return (
GetPlayerVirtualWorld(playerid) == GetVehicleVirtualWorld(vehicleid)) && IsPlayerInRangeOfPoint(playerid3.5fXfYfZ);
}
IsPlayerNearHood(playeridvehicleid) {
    static
        
Float:fX,
        
Float:fY,
        
Float:fZ;
    
GetVehicleHood(vehicleidfXfYfZ);
    return (
GetPlayerVirtualWorld(playerid) == GetVehicleVirtualWorld(vehicleid)) && IsPlayerInRangeOfPoint(playerid3.0fXfYfZ);
}
GetVehicleBoot(vehicleid, &Float:x, &Float:y, &Float:z) {
    if (!
GetVehicleModel(vehicleid) || vehicleid == INVALID_VEHICLE_ID)
        return (
0.00.00.0), 0;
    static
        
Float:pos[7]
    ;
    
GetVehicleModelInfo(GetVehicleModel(vehicleid), VEHICLE_MODEL_INFO_SIZEpos[0], pos[1], pos[2]);
    
GetVehiclePos(vehicleidpos[3], pos[4], pos[5]);
    
GetVehicleZAngle(vehicleidpos[6]);
    
pos[3] - (floatsqroot(pos[1] + pos[1]) * floatsin(-pos[6], degrees));
    
pos[4] - (floatsqroot(pos[1] + pos[1]) * floatcos(-pos[6], degrees));
     
pos[5];
    return 
1;
}
GetVehicleHood(vehicleid, &Float:x, &Float:y, &Float:z) {
    if (!
GetVehicleModel(vehicleid) || vehicleid == INVALID_VEHICLE_ID)
        return (
0.00.00.0), 0;
    static
        
Float:pos[7]
    ;
    
GetVehicleModelInfo(GetVehicleModel(vehicleid), VEHICLE_MODEL_INFO_SIZEpos[0], pos[1], pos[2]);
    
GetVehiclePos(vehicleidpos[3], pos[4], pos[5]);
    
GetVehicleZAngle(vehicleidpos[6]);
    
pos[3] + (floatsqroot(pos[1] + pos[1]) * floatsin(-pos[6], degrees));
    
pos[4] + (floatsqroot(pos[1] + pos[1]) * floatcos(-pos[6], degrees));
     
pos[5];
    return 
1;

This should give you an idea on how to detect the door positions. Keep in mind that when doing so you have to check for quite the possible cases: is the player hitting a bike, is the player hitting a golf cart, How many doors does the vehicle have, etc..

The following topics seem also very interesting:
Trigonometry by Mauze: https://sampforum.blast.hk/showthread.php?tid=270427
Extended vehicle functions by Emmet_: https://sampforum.blast.hk/showthread.php?tid=486060


Re: Detect if player is hitting a door - AidanRO - 13.03.2016

Thanks for that. Basically, that should work with OnPlayerKeyStateChange and detect if player is in the "fisting" animation, right?


Re: Detect if player is hitting a door - AndySedeyn - 13.03.2016

You can check if the player has a gun out or just his fist. You can then check if the player is attacking in OnPlayerKeyStateChange and then check what the player's position is.

> Player presses RMB (or the assigned key to attack)
> Does player have his fist out, or a particular weapon?
> Is the player in range of a vehicle's door?

Here is the wiki page to all the keys detectable by SAMP: https://sampwiki.blast.hk/wiki/Keys
And the following snippet holds three macro functions to detect when a player is tapping, holding or just released a key:
PHP код:
#define HOLDING(%0)             ((newkeys & (%0)) == (%0))
#define PRESSED(%0)             (((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))
#define RELEASED(%0)         (((newkeys & (%0)) != (%0)) && ((oldkeys & (%0)) == (%0))) 
I don't know who the credits go to, but I got these from Abagail's topic: https://sampforum.blast.hk/showthread.php?tid=491980


Re: Detect if player is hitting a door - AidanRO - 13.03.2016

Job done.
Thanks a lot Andy. I managed to detect the each doors's pos easy.

God bless.


Re: Detect if player is hitting a door - AidanRO - 14.03.2016

This can be closed. Everything has been done.