Garry's Mod Wiki

Revision Difference

ents.FindInCone#514541

<function name="FindInCone" parent="ents" type="libraryfunc">⤶ <description>⤶ Finds and returns all entities within the specified cone. Only entities whose <page>Entity:WorldSpaceCenter</page> is within the cone are considered to be in it.⤶ ⤶ The "cone" is actually a conical "slice" of an axis-aligned box (see: <page>ents.FindInBox</page>). The image to the right shows approximately how this function would look in 2D. Due to this, the entity may be farther than the specified range!⤶ ⤶ <note>Clientside entities will not be returned by this function.</note>⤶ ⤶ <warning>If there are more than 512 entities in the axis-aligned box around the origin, then the result may be incomplete!</warning>⤶ </description>⤶ <realm>Shared</realm>⤶ <args>⤶ <arg name="origin" type="Vector">The tip of the cone.</arg>⤶ <arg name="normal" type="Vector">Direction of the cone.</arg>⤶ <arg name="range" type="number">The range of the cone/box around the origin. &amp;amp;lt;!-- The function internally adds 1 to this argument before using it. --&amp;amp;gt;</arg>⤶ <arg name="angle_cos" type="number">The [cosine](/gmod/math/cos) of the angle between the center of the cone to its edges, which is half the overall angle of the cone.&#xA;&#xA;1 makes a 0&#xB0; cone, 0.707 makes approximately 90&#xB0;, 0 makes 180&#xB0;, and so on.</arg>⤶ </args>⤶ <rets>⤶ <ret name="" type="table">A table of all found <page>Entity</page>s.</ret>⤶ </rets>⤶ </function>⤶ ⤶ <example>⤶ <description>An example usage of this function. This example shows which entities are being returned by the function with red lines and the range with white transparent box.</description>⤶ <code>⤶ local mat = Material( "models/shiny" )⤶ mat:SetFloat( "$alpha", 0.5 )⤶ ⤶ hook.Add( "PostDrawOpaqueRenderables", "conetest", function()⤶ local size = 200⤶ local dir = LocalPlayer():GetAimVector()⤶ local angle = math.cos( math.rad( 15 ) ) -- 15 degrees⤶ local startPos = LocalPlayer():EyePos()⤶ ⤶ local entities = ents.FindInCone( startPos, dir, size, angle )⤶ ⤶ -- draw the outer box⤶ local mins = Vector( -size, -size, -size )⤶ local maxs = Vector( size, size, size )⤶ ⤶ render.SetMaterial( mat )⤶ render.DrawWireframeBox( startPos, Angle( 0, 0, 0 ), mins, maxs, color_white, true )⤶ render.DrawBox( startPos, Angle( 0, 0, 0 ), -mins, -maxs, color_white )⤶ ⤶ -- draw the lines⤶ for id, ent in pairs( entities ) do⤶ render.DrawLine( ent:WorldSpaceCenter() - dir * ( ent:WorldSpaceCenter()-startPos ):Length(), ent:WorldSpaceCenter(), Color( 255, 0, 0 ) )⤶ end⤶ end )⤶ </code>⤶ ⤶ </example>