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

Cast a ray starting at point a and ending in point b?

Asked by 8 years ago

So i have an AI that will cast a ray starting from the AIs torso and ending at a enemys torso, to detect rather there's any part between them.

How do i calculate the direction of the targets torso from the AIs torso?

2 answers

Log in to vote
0
Answered by 8 years ago

The link here might help you with what you're trying to accomplish.

--script in the AI's torso

local character = PLAYER --a reference to the character who's torso you're trying to detect.
local dist = 30 --Distance of the ray, i.e. the distance the AI can see the player from.

local ray = Ray.new(script.Parent.Position,(character.Torso.Position - script.Parent.Position).unit*dist) --A new ray, the first parameter is the start, which is the AI's torso, and the second parameter is the endpoint of the ray. The first part of the second parameter can be considered as the 3d slope, or direction the ray will travel in. The second part, dist, is how far we travel down that slope.

local part,position = workspace:FindPartOnRay(ray,script.Parent.Parent,false,true) --find the first part the ray touches, ignoring any parts belonging to the AI

if part:IsDescendantOf(character) then --if the part hit belongs to the character then print the character's name
    print("I see "..character.Name)
end
0
I already figured it out. Thanks and accepting the answer anyway. Also you forgot a comma between "script.Parent.Position" and "(character.Torso.Position - script.Parent.Position).unit*dist" in line 6. DragonOfWar900 397 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

I found out.

local start = script.Parent.Torso.CFrame.p
local finish = target.Value.Torso.CFrame.p
local ray = Ray.new(start,(finish - start).unit *50)

--Casts ray
local Raypart,pos = game.Workspace:FindPartOnRay(ray)

Answer this question