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)

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}
	local trace = util.TraceLine(tracedata)
	if trace.HitWorld then
		local barrel=ents.Create("unit")
		barrel:SetNWEntity("owner", ply)
		barrel:SetNWString("owner_nick", ply:Nick())
concommand.Add("UnitSpawn", UnitSpawn)

Any help at all will be greatly appreciated.

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

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

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.
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



Use :

gui.ScreenToVector( gui.MousePos() )

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.

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()