Can't seem to get util.TraceLine masks/collision group/filter working!
7 replies, posted
Hey all,
I'm doing two simple traces to detect whether a player can be seen or not, in order to draw a circle beneath their feet:
https://files.facepunch.com/forum/upload/851/8a218670-0235-4429-99d5-88188c1a2bab/image.png
I've been playing around for hours with the collisiongroup and mask values of the trace table:
local ply_screen_pos = ply:GetPos():ToScreen()
if (not ply_screen_pos.visible) then
continue
end
local ply_eye_tr = util.TraceLine({
start = LocalPlayer():EyePos(),
endpos = ply:EyePos(),
mask = CONTENTS_SOLID + CONTENTS_OPAQUE + CONTENTS_TRANSLUCENT + CONTENTS_MOVEABLE + CONTENTS_BLOCKLOS,
filter = local_ply_tbl,
collisiongroup = COLLISION_GROUP_INTERACTIVE_DEBRIS
})
local ply_feet_tr = util.TraceLine({
start = LocalPlayer():GetPos() + Vector(0,0,1),
endpos = ply:GetPos() + Vector(0,0,1),
mask = CONTENTS_SOLID + CONTENTS_OPAQUE + CONTENTS_TRANSLUCENT + CONTENTS_MOVEABLE + CONTENTS_BLOCKLOS,
filter = local_ply_tbl,
collisiongroup = COLLISION_GROUP_INTERACTIVE_DEBRIS
})
if ((ply_feet_tr.Hit and ply_feet_tr.Entity ~= ply) and (ply_eye_tr.Hit and ply_eye_tr.Entity ~= ply)) then continue end
print("Drawing", math.random(1,999))
However, the traces still appear to clip through doors, props, etc., but not the world itself.
I first started off like this:
local ply_eye_tr = util.TraceLine({
start = LocalPlayer():EyePos(),
endpos = ply:EyePos(),
mask = MASK_OPAQUE,
filter = local_ply_tbl
})
local ply_feet_tr = util.TraceLine({
start = LocalPlayer():GetPos() + Vector(0,0,1),
endpos = ply:GetPos() + Vector(0,0,1),
mask = MASK_OPAQUE,
filter = local_ply_tbl
})
But, for some reason, even though the description of MASK_OPAQUE is perfect for what I would like to do, it doesn't work as intended - the traces still go through props, doors, etc.
How can I get this right?
local function CanSeePlayer(ply)
return util.TraceLine({
start = LocalPlayer():GetShootPos(),
endpos = ply:LocalToWorld(ply:OBBCenter()),
filter = {ply, LocalPlayer()},
mask = MASK_SHOT
}).Fraction == 1
end
Thanks but it doesn't work as intended!
https://files.facepunch.com/forum/upload/851/d20862b4-124d-482f-a323-61b950ef05ee/image.png
I don't think you understand what trace line does, it traces a LINE. If a line hits something, it returns that entity. It doesn't magically tell you if a player can see another player. It's not very suitable for determining if a player sees another player on its own.
What you may want to do is trace to each corner of player's bbox but even then it won't really cover what you uwant to do.
af here you go
local CanSeePlayer
do
local IsValid = IsValid
local TraceLine = util.TraceLine
local output, filter, localplayer = {}, {}
local traceTbl = {output = output, filter = filter, mask = MASK_SHOT}
function CanSeePlayer(ply)
if localplayer == nil then
localplayer = IsValid(LocalPlayer()) && LocalPlayer() || nil
filter[1] = localplayer
return false
end
filter[2] = ply
traceTbl.start = localplayer:GetShootPos()
traceTbl.endpos = ply:LocalToWorld(ply:OBBMaxs())
TraceLine(traceTbl)
if (output.Fraction == 1) then return true end
traceTbl.endpos = ply:LocalToWorld(ply:OBBCenter())
TraceLine(traceTbl)
if (output.Fraction == 1) then return true end
traceTbl.endpos = ply:LocalToWorld(ply:OBBMins())
TraceLine(traceTbl)
return output.Fraction == 1
end
end
Of course I know that it traces a fuckin line. I'm not looking for a magical solution, because there isn't one... tracing a line is one of the most common methods of doing this... wtf, of all people, you should know this. I should've made myself clearer that I'm looking for a solution to the "trace not hitting props and doors" issue, not the player seeing check. And yeah, I don't really wanna go for the player's bbox as that could be pretty inaccurate. I want my LINE to hit props, doors and etc., but at the moment, for whatever reason, it doesn't, even though I am using MASK_OPAQUE. But I guess I opened this thread with the open question of how people go about solving this issue in a more general sense, which you clearly misunderstood and just assumed I'm retarded.
Is that efficient? 🤔
I'll be trying this almost every frame, possibly multiple times. I think maybe only two traces is necessary.
Bitches solution here perhaps? Detect if player can see you on their screen?
My apologies, I did indeed misunderstand the problem. Don't use any masks at all and it should work.
Sorry, you need to Log In to post a reply to this thread.