Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Detecting objects within first person view using Rays, how?

Asked by 11 years ago

I plan on creating a method that will detect when certain objects are within your sight (On your screen, so to speak.). It is important to note that my game will be in a permanent First Person View state, so I have created a Ray, who's origin is the characters head, and the direction is the heads lookVector. Anyway, using that Ray it was easy for me to detect what object I am staring at right INFRONT of me, but it will not detect the objects in the places my mouse is not pointing at. Do you understand? What I want is that somehow, with Rays or without, i'll be able to detect whether or not a certain object is within the players sight. Thank's in advance!

0
This question requires a really complex solution for optimal speed. Tomorrow, I'm going to make a place for you to see actual code in, then link it here as an Answer with some explanations as to how it works. It's really late for me right now, sorry. adark 5487 — 11y
0
That'd be nice :O NutsNWaffles 135 — 11y

1 answer

Log in to vote
2
Answered by
wazap 100
11 years ago
01local listOfItems = {workspace.Object1, workspace.Object2}
02repeat wait() until game.Players.LocalPlayer.Character
03local head = game.Players.LocalPlayer.Character.Head
04local AngleOfSight = 15 --Everything within 15 degrees will be seen
05function inSight(hitPos, headPos)
06    local scan = head.CFrame.lookVector
07    local vect = hitPos-headPos
08    return  math.acos((vect.X*scan.X+vect.Y*scan.Y+vect.Z*scan.Z)/vect.magnitude)<AngleOfSight
09end
10 
11while wait() do
12    local itemsInSight = {}
13    local startPos = head.Position
14    for i, v in pairs(listOfItems) do
15        local ray = Ray.new(head.Position, (v.Position-head.Position).unit*999)
View all 21 lines...
0
I'll check this soon enough, however I would like you to explain what you did on line 8, thanks! NutsNWaffles 135 — 11y
0
Worked just fine when I set the AngleOfSight to 1. Thanks! Though I would still like to know what you did on line 8. NutsNWaffles 135 — 11y
0
Its math. If the item is within the angle range, it'll return true, otherwise it'll return false wazap 100 — 11y
Ad

Answer this question