GetMouseHitLocation: Difference between revisions
From Onset Developer Wiki
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
{{Info|Function|Client|1.0}} | {{Info|Function|Client|1.0}} | ||
{{FuncDescription| | {{FuncDescription|Gets the X, Y, Z world location below the mouse cursor. This basically traces from the position of the mouse cursor to the next colliding geometry in world space. | ||
To get the entity use [[GetMouseHitEntity]].}} | |||
{{FuncSyntax|GetMouseHitLocation()}} | {{FuncSyntax|GetMouseHitLocation()}} | ||
Line 8: | Line 10: | ||
{{FuncNoParam}} | {{FuncNoParam}} | ||
{{FuncReturnValue| | {{FuncReturnValue|Returns 4 float values, X, Y, Z and distance.}} | ||
== Example == | == Example == | ||
<syntaxhighlight lang="Lua> | |||
function OnKeyPress(key) | |||
if key == "Left Mouse Button" then | |||
if IsEditMode then | |||
local x, y, z, distance = GetMouseHitLocation() | |||
AddPlayerChat("Hit: "..x..", "..y..", "..z..", "..distance) | |||
-- if selected location is not valid (i.e. clicking sky) 0.0 is returned. we set it to something high so the check is triggered later on. | |||
if distance == 0.0 then | |||
distance = 999999.9 | |||
end | |||
SelectedLoc.x = x | |||
SelectedLoc.y = y | |||
SelectedLoc.z = z | |||
SelectedLoc.distance = distance | |||
SelectedLoc.isValid = true | |||
local EntityType, EntityId = GetMouseHitEntity() | |||
if (EntityType == HIT_OBJECT) then | |||
if (EntityId ~= 0 and SelectedObject ~= EntityId) then | |||
SelectEditorObject(EntityId) | |||
end | |||
end | |||
end | |||
end | |||
end | |||
AddEvent("OnKeyPress", OnKeyPress) | |||
</syntaxhighlight> | |||
{{RelatedFunctions}} | {{RelatedFunctions}} | ||
{{Template:ClientMouseFunctions}} | {{Template:ClientMouseFunctions}} |
Revision as of 20:11, 30 August 2020
Description
Gets the X, Y, Z world location below the mouse cursor. This basically traces from the position of the mouse cursor to the next colliding geometry in world space.
To get the entity use GetMouseHitEntity.
Syntax
GetMouseHitLocation()
Parameters
- This function has no parameters.
Return Value
- Returns 4 float values, X, Y, Z and distance.
Example
function OnKeyPress(key)
if key == "Left Mouse Button" then
if IsEditMode then
local x, y, z, distance = GetMouseHitLocation()
AddPlayerChat("Hit: "..x..", "..y..", "..z..", "..distance)
-- if selected location is not valid (i.e. clicking sky) 0.0 is returned. we set it to something high so the check is triggered later on.
if distance == 0.0 then
distance = 999999.9
end
SelectedLoc.x = x
SelectedLoc.y = y
SelectedLoc.z = z
SelectedLoc.distance = distance
SelectedLoc.isValid = true
local EntityType, EntityId = GetMouseHitEntity()
if (EntityType == HIT_OBJECT) then
if (EntityId ~= 0 and SelectedObject ~= EntityId) then
SelectEditorObject(EntityId)
end
end
end
end
end
AddEvent("OnKeyPress", OnKeyPress)