OnClientVehicleCollision: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
Line 1: Line 1:
{{New feature/item|3.0130|1.3|3653|
Added in MTA SA 1.3
}}
{{Client event}}
{{Client event}}
__NOTOC__  
__NOTOC__  
Line 9: Line 6:
'''Note:''' ''theHitElement'' will be nil or false if it's a default SA object and it will trigger twice for vehicles because one vehicle hit another and one got hit by another.
'''Note:''' ''theHitElement'' will be nil or false if it's a default SA object and it will trigger twice for vehicles because one vehicle hit another and one got hit by another.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
element theHitElement, float force, int bodypart, float collisionX, float collisionY, float collisionZ, float velocityX, float velocityY, float velocityZ, float hitElementForce, int model
element theHitElement, float force, int bodypart, float collisionX, float collisionY, float collisionZ, float normalX, float normalY, float normalZ, float hitElementForce, int model
</syntaxhighlight>
</syntaxhighlight>
*'''theHitElement:''' the entity that took damage
*'''theHitElement:''' the other entity, or nil if the vehicle collided with the world
*'''force:''' the force of the damage (Note: this is NOT the damage it is a force value which is then multiplied by the vehicles collision damage multiplier. for an example of this see below)  
*'''force:''' the force of the damage (Note: this is NOT the damage it is a force value which is then multiplied by the vehicles collision damage multiplier. for an example of this see below)  
*'''bodyPart:''' the bodypart that hit the other element
*'''bodyPart:''' the bodypart that hit the other element
*'''collisionX:''' the X position the collision took place
*'''collisionX/Y/Z:''' the position the collision took place
*'''collisionY:''' the Y position the collision took place
*'''normalX/Y/Z:''' the surface normal of the hit object
*'''collisionZ:''' the Z position the collision took place
*'''velocityX:''' the X velocity of the source before the collision took place
*'''velocityY:''' the Y velocity of the source before the collision took place
*'''velocityZ:''' the Z velocity of the source before the collision took place
*'''hitElementforce:''' 0 for non vehicles or the force of the other vehicle
*'''hitElementforce:''' 0 for non vehicles or the force of the other vehicle
*'''model:''' model of the hit element (useful to detect building collisions as hitElement will be nil)
*'''model:''' model of the hit element (useful to detect building collisions as hitElement will be nil)
Line 29: Line 22:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
addEventHandler("onClientVehicleCollision", root,
addEventHandler("onClientVehicleCollision", root,
     function(collider,force, bodyPart, x, y, z, vx, vy, vz)
     function(collider,force, bodyPart, x, y, z, nx, ny, nz)
         if ( source == getPedOccupiedVehicle(localPlayer) ) then
         if ( source == getPedOccupiedVehicle(localPlayer) ) then
             -- force does not take into account the collision damage multiplier (this is what makes heavy vehicles take less damage than banshees for instance) so take that into account to get the damage delt
             -- force does not take into account the collision damage multiplier (this is what makes heavy vehicles take less damage than banshees for instance) so take that into account to get the damage dealt
             local fDamageMultiplier = getVehicleHandling(source).collisionDamageMultiplier
             local fDamageMultiplier = getVehicleHandling(source).collisionDamageMultiplier
             -- Create a marker (I've scaled this down to 1% of the actual damage otherwise we will get huge markers)
             -- Create a marker (Scaled down to 1% of the actual damage otherwise we will get huge markers)
             local m = createMarker(x, y, z, "corona", force * fDamageMultiplier * 0.01, 0, 9, 231)
             local m = createMarker(x, y, z, "corona", force * fDamageMultiplier * 0.01, 0, 9, 231)
             -- Destroy the marker in 2 seconds
             -- Destroy the marker in 2 seconds
Line 50: Line 43:
         if ( source == getPedOccupiedVehicle(localPlayer) ) then
         if ( source == getPedOccupiedVehicle(localPlayer) ) then
             -- if our hit element is nil (we just hit an SA map object) set knockOff to true, else set to false
             -- if our hit element is nil (we just hit an SA map object) set knockOff to true, else set to false
             local knockOff = hit and false or true  
             local knockOff = hit and false or true
    
    
             -- update our can be knocked off bike status accordingly
             -- update our can be knocked off bike status accordingly

Revision as of 05:41, 12 July 2012

This event is triggered when a vehicle collides with peds/vehicles/objects.

Parameters

Note: theHitElement will be nil or false if it's a default SA object and it will trigger twice for vehicles because one vehicle hit another and one got hit by another.

element theHitElement, float force, int bodypart, float collisionX, float collisionY, float collisionZ, float normalX, float normalY, float normalZ, float hitElementForce, int model
  • theHitElement: the other entity, or nil if the vehicle collided with the world
  • force: the force of the damage (Note: this is NOT the damage it is a force value which is then multiplied by the vehicles collision damage multiplier. for an example of this see below)
  • bodyPart: the bodypart that hit the other element
  • collisionX/Y/Z: the position the collision took place
  • normalX/Y/Z: the surface normal of the hit object
  • hitElementforce: 0 for non vehicles or the force of the other vehicle
  • model: model of the hit element (useful to detect building collisions as hitElement will be nil)

Source

The source of this event is the vehicle that collided with something.

Example

addEventHandler("onClientVehicleCollision", root,
    function(collider,force, bodyPart, x, y, z, nx, ny, nz)
         if ( source == getPedOccupiedVehicle(localPlayer) ) then
             -- force does not take into account the collision damage multiplier (this is what makes heavy vehicles take less damage than banshees for instance) so take that into account to get the damage dealt
             local fDamageMultiplier = getVehicleHandling(source).collisionDamageMultiplier
             -- Create a marker (Scaled down to 1% of the actual damage otherwise we will get huge markers)
             local m = createMarker(x, y, z, "corona", force * fDamageMultiplier * 0.01, 0, 9, 231)
             -- Destroy the marker in 2 seconds
             setTimer(destroyElement, 2000, 1, m)
         end
    end
)

-- This code works because onClientVehicleCollision is triggered before any SA reaction to the collision, therefore we can update the knocked off bike status just before the collision and stop the falling off effect happening :)
addEventHandler("onClientVehicleCollision", root,
    function ( hit ) 
        -- firstly did we trigger this event
        if ( source == getPedOccupiedVehicle(localPlayer) ) then
            -- if our hit element is nil (we just hit an SA map object) set knockOff to true, else set to false
            local knockOff = hit and false or true
  
            -- update our can be knocked off bike status accordingly
            setPedCanBeKnockedOffBike(localPlayer, knockOff) 
        end
    end
)

See Also

Client vehicle events


Client event functions