GetGroundPosition: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Example fixed)
(Fix examples header)
 
(8 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{Needs_Checking|what's the maximum distance from the player we can check?}}
__NOTOC__
__NOTOC__
This function gets the Z level of the highest ground below a point. It is required that the point is near enough to the local player so that it's within the area where world map data is loaded.
{{Client function}}
This function gets the Z level of the highest ground below a point.  
 
It is required that the point is near enough to the local player so that it's within the area where collision data is loaded. If this is not the case, an incorrect position will be returned.


==Syntax==  
==Syntax==  
Line 16: Line 18:
Returns a float with the highest ground-level Z coord if parameters are valid, ''0'' if the point you tried to test is outside the loaded world map, ''false'' otherwise.
Returns a float with the highest ground-level Z coord if parameters are valid, ''0'' if the point you tried to test is outside the loaded world map, ''false'' otherwise.


==Example==  
==Examples==  
This clientside function determines if a player is under a ceiling or not.
This clientside function determines if a player is under a ceiling or not.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
Line 24: Line 26:
--we'll check for ground level at the player's position, and also 500 units over him.
--we'll check for ground level at the player's position, and also 500 units over him.
--if these ground levels match, it must mean there were no obstacles (such as a ceiling) over the player,
--if these ground levels match, it must mean there were no obstacles (such as a ceiling) over the player,
if getGroundPosition ( px, py, pz ) == getGroundPosition ( px, py, pz + 500 ) then
return getGroundPosition ( px, py, pz ) ~= getGroundPosition ( px, py, pz + 530 )
-- so the player is not under cover
end
return false
</syntaxhighlight>
--otherwise, there was an object over him,
 
else
This clientside function returns the distance between you and the ground (if there is)
-- so the player is under cover
<syntaxhighlight lang="lua">
return true
function getGroundDistance( thePlayer, zHeight  )
end
local x,y,z = getElementPosition(thePlayer) -- get position
    local groundPosition = getGroundPosition(x,y,z) -- get default ground pos
local hit, x1, y1, groundPosition = processLineOfSight(x,y,z+1,x,y,z-(zHeight+0.7))  -- using zHeight get a better ground position
local distance = getDistanceBetweenPoints3D(x, y, z, x,y, (groundPosition or z))  -- get distance between both positions
return distance -- return the distance
end
end
-- Some cool examples: 
addCommandHandler("gdistance",function(_, amount)
    local distance = getGroundDistance(localPlayer, tonumber(amount))
    outputChatBox("The distance to reach ground is: " .. distance)
end)
addCommandHandler("do_im_flying",function()
    local distance = getGroundDistance(localPlayer, 20)
    if distance > 1 then
        outputChatBox("Superman!! the distance between you and ground is: " .. distance)
    else
        outputChatBox("naah")
    end
end)
</syntaxhighlight>
</syntaxhighlight>


==See Also==
==See Also==
{{Client_world_functions}}
{{Client_world_functions}}
[[ru:getGroundPosition]]

Latest revision as of 13:46, 8 June 2025

This function gets the Z level of the highest ground below a point.

It is required that the point is near enough to the local player so that it's within the area where collision data is loaded. If this is not the case, an incorrect position will be returned.

Syntax

float getGroundPosition ( float x, float y, float z )

Required Arguments

  • x: A floating point number representing the X world coordinate of the point.
  • y: A floating point number representing the Y world coordinate of the point.
  • z: A floating point number representing the Z world coordinate of the point.

Returns

Returns a float with the highest ground-level Z coord if parameters are valid, 0 if the point you tried to test is outside the loaded world map, false otherwise.

Examples

This clientside function determines if a player is under a ceiling or not.

function isPlayerUnderCover ( thePlayer )
	--we get the player's position
	local px, py, pz = getElementPosition ( thePlayer )
	--we'll check for ground level at the player's position, and also 500 units over him.
	--if these ground levels match, it must mean there were no obstacles (such as a ceiling) over the player,
	return getGroundPosition ( px, py, pz ) ~= getGroundPosition ( px, py, pz + 530 )
end

This clientside function returns the distance between you and the ground (if there is)

function getGroundDistance( thePlayer, zHeight  )
	local x,y,z = getElementPosition(thePlayer) -- get position
    local groundPosition = getGroundPosition(x,y,z) -- get default ground pos 
	local hit, x1, y1, groundPosition = processLineOfSight(x,y,z+1,x,y,z-(zHeight+0.7))  -- using zHeight get a better ground position
	local distance = getDistanceBetweenPoints3D(x, y, z, x,y, (groundPosition or z))  -- get distance between both positions 
	return distance -- return the distance
end

-- Some cool examples:  
addCommandHandler("gdistance",function(_, amount)
    local distance = getGroundDistance(localPlayer, tonumber(amount))
    outputChatBox("The distance to reach ground is: " .. distance)
end)

addCommandHandler("do_im_flying",function()
    local distance = getGroundDistance(localPlayer, 20)
    if distance > 1 then 
        outputChatBox("Superman!! the distance between you and ground is: " .. distance)
    else 
        outputChatBox("naah")
    end 
end)

See Also