Get the origin of the entity the player is looking at.

I’ve just recently begun learning LUA coding and im a bit befuddled.

I’m trying to make it so the origin of an entity the player is looking at (IE: whatever is in there crosshair) is output to a variable and then using effectdata it creates an explosion effect at the origin. This should only be based on the crosshair as this is being made for a SWEP. Any ideas on how I would go about doing this?

I’d really appreciate a code snippet if possible.

Thanks.

Based… On the crosshair? What you want to do is to fire a ray (Called raytracing. When firing a ray you ‘trace’. It’s one of the basic functions of the engine) in the direction the player is looking. This returns a traceres table and…

Well, I guess the wiki explains it better.

http://wiki.garrysmod.com/?title=TraceRes and
http://wiki.garrysmod.com/?title=Util.TraceLine or http://wiki.garrysmod.com/?title=Player.GetEyeTrace

Just like esalaka said! :smile:

[lua]local tr = ply:GetEyeTrace()
if tr.HitEnt then pos = tr.HitPos end
print(pos)[/lua]
:eng101:

Actually…
[lua]
local tr = ply:GetEyeTrace() --'Tis cheaper than using TraceLine
– ply:GetEyeTrace results are cached for each frame, IIRC

local pos = Vector( 0, 0, 0 )
if tr.HitEnt then pos = tr.Entity:GetPos() end
print( pos )
[/lua]

[lua] local ent = ply:GetEyeTrace().Entity
if IsValid(ent) then
print(ent:GetPos())
end[/lua]

So if i were to do this using a SWEP:
[lua]
local tr = ply:GetEyeTrace()
local pos = Vector( 0, 0, 0 )
if tr.HitEnt then pos = tr.Entity:GetPos() end
effectdata:SetStart(pos)
effectdata:SetOrigin(pos)
effectdata:SetScale(1)
util.Effect(“Explosion”, effectdata)
tr.Entity:Remove()
[/lua]

Would this create a SWEP that could be used to remove entities?
The SWEP coding is already done, i just need to setup the target, removing it, and the effect it does when removed.

To be more specific i’m trying to create a SWEP that can be used to remove a targeted entity named “money_printer” in the gamemode DarkRP

I hope i’m on the right track, thanks for the feedback so far everyone. I really appreciate it.

[lua]
local tr = self.Owner:GetEyeTrace() --In a swep ply is self.Owner
local pos = Vector( 0, 0, 0 )
if tr.Entity and tr.Entity:GetClass() == “money_printer” then pos = tr.Entity:GetPos() end
effectdata:SetStart(pos)
effectdata:SetOrigin(pos)
effectdata:SetScale(1)
util.Effect(“Explosion”, effectdata)
tr.Entity:Remove()
[/lua]
:smile:

[lua]
local tr = self.Owner:GetEyeTrace() --In a swep ply is self.Owner
if tr.Entity == nil or tr.Entity:GetClass() ~= “money_printer” then return; end
local pos = tr.Entity:GetPos()
effectdata:SetStart(pos)
effectdata:SetOrigin(pos)
effectdata:SetScale(1)
util.Effect(“Explosion”, effectdata)
tr.Entity:Remove()
[/lua]

:science:

Both Crazy Quebec’s and Gbps’s worked perfectly. Thanks a million guys!

Use mine, it doesn’t save an extra variable it doesn’t need to save.

[editline]11:31AM[/editline]

:v:

Right, I should’ve edited his stuff out I reckon. :frown: :ninja: