SetElementCollisionsEnabled: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
m (tweaked the example)
Line 26: Line 26:
local vehicleX, vehicleY, vehicleZ = getElementPosition(v)
local vehicleX, vehicleY, vehicleZ = getElementPosition(v)
-- get the distance between the player and the vehicle:
-- get the distance between the player and the vehicle:
local distance = math.sqrt((vehicleX - playerX)^2 + (vehicleY - playerY)^2 + (vehicleZ - playerZ)^2)
local distance = getDistanceBetweenPoints3D(vehicleX, vehicleY, vehicleZ, playerX, playerY, playerZ)
if (distance <= maxDistance) then
if (distance <= maxDistance) then
-- disable collisions for the vehicle
-- disable collisions for the vehicle

Revision as of 00:33, 13 February 2010

This function can disable or enable an element's collisions. An element without collisions does not interact with the physical environment and remains static.
Note: This function has unwanted effects on vehicles with drivers at the moment.

Syntax

bool setElementCollisionsEnabled ( element theElement, bool enabled ) 

Required Arguments

  • theElement: The element you wish to set the collisions of
  • enabled: A boolean to indicate whether collisions are enabled (true) or disabled (false)

Returns

Returns true if the collisions were set succesfully, false otherwise.

Example

This example disables collisions for all vehicles within a certain radius of a player:

Click to collapse [-]
Client
function disableVehicleCollisionsNearPlayer(thePlayer, maxDistance)
	local playerX, playerY, playerZ = getElementPosition(thePlayer)
	local vehicles = getElementsByType("vehicle")
	for k,v in ipairs(vehicles) do
		local vehicleX, vehicleY, vehicleZ = getElementPosition(v)
		-- get the distance between the player and the vehicle:
		local distance = getDistanceBetweenPoints3D(vehicleX, vehicleY, vehicleZ, playerX, playerY, playerZ)
		if (distance <= maxDistance) then
			-- disable collisions for the vehicle
			setElementCollisionsEnabled(v, false)
		end
	end
end

See Also