Mouse Position -> World Position

So, I have hit a big roadblock, and can’t get this to work. I’ve tried countless things over the past week but with no luck whatsoever.
The client’s mouse is freed with gui.EnableScreenClicker(true).
I then use local MousePositionVector = gui.ScreenToVector(gui.MousePos()) to get a vector.
I then make a trace with the start position ply:GetShootPos() and the end position ply:GetShootPos()+(MousePositionVector*1000)
the trace.HitPos tends to be random, and basically just nonsense.

here’s the code.


function GM:GUIMousePressed(mouse)
	if mouse == 107 then
		local ang = gui.ScreenToVector(gui.MousePos())
		LocalPlayer():ConCommand("UnitSpawn "..ang.x.." "..ang.y.." "..ang.y)
	end
end


function UnitSpawn(ply, cmd, args)
	local pos = ply:GetShootPos()
	local ang = Vector(args[1], args[2], args[3])
	local tracedata = {}
	tracedata.start = pos
	tracedata.endpos = pos+(ang*1000)
	for k,v in pairs(ents.FindByClass("func_wall")) do
		tracedata.filter = {ply, v}
	end
	local trace = util.TraceLine(tracedata)
	if trace.HitWorld then
		local barrel=ents.Create("unit")
		barrel:SetModel("models/props_c17/oildrum001.mdl")
		barrel:SetPos(Vector(trace.HitPos.x,trace.HitPos.y,trace.HitPos.z+1))
		barrel:Spawn()
		barrel:SetNWEntity("owner", ply)
		barrel:SetNWString("owner_nick", ply:Nick())
	end
end
concommand.Add("UnitSpawn", UnitSpawn)

Any help at all will be greatly appreciated.

You messed up the filter part
[lua]
tracedata.filter = {ply}
for k,v in pairs(ents.FindByClass(“func_wall”)) do
table.insert(tracedata.filter, v)
end
[/lua]

Okay, that helped but I’m still having the same troubles as before

http://www.facepunch.com/showpost.php?p=22370248&postcount=26

That should help. Check the whole thread for other info.

Shouldn’t you be using the player’s Eye position instead of the Shoot position for the origin?

The Eye position and shoot position are the same. I printed both in the console, and they were identical.

http://www.facepunch.com/showpost.ph...8&postcount=26
as for that thread, I read through and tried all the suggestions, but the all ended the same, with no luck. I even copied the whole function into my code, just to see if it would work and it didn’t.
Someone must have done this before, and it must be possible, I just don’t know where to turn.

i found a function not so long ago, one sec

[editline]03:07PM[/editline]

**[Gui.ScreenToVector

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Gui.ScreenToVector)**

Use :

[LUA]
gui.ScreenToVector( gui.MousePos() )
[/LUA]

Returns :

Vector ( normalized vector direction, not world coordinates )

I’m already using that function, and I really thought I had solved it as soon as I saw the wiki page, but once I used it, it just didn’t work. It returned a normalized vector, and i used that vector to define the end position of my trace, but it just did really random stuff which made no sense.

It doesn’t work like that. The normalized vector it returns is the direction, not the end position for a trace. The correct way to use it is:

[lua]local vec = gui.ScreenToVector( gui.MousePos() )
local traceResult = util.TraceLine{
start = ply:GetShootPos(),
endpos = ply:GetShootPos() + vec * 4096 – Traces 4096 units away from the player.
}[/lua]

in other words, what was on the wiki :3

I did that, but still its not working. I’ll post a quick xfire vid to show you what exactly it is doing

EDIT: Here’s the vid

I just had it print the trace.HitPos in the console, and I set my players position to that vector to show you where it thinks that I clicked on the world.

Have you tied Player:EyePos()? I just did exactly what your doing with the screen to world stuff and mine works.

Don’t forget to add the player to the filter.

yes, i tried player:EyePos()