Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

1 comment

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.

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.