Need help with understanding ray stuff

Could someone explain to me how I could make a simple ray that detects player/root part and have a certain range ?
So far I was looking at some of those ai scripts and detectors but they are a bit too complicated for me I just can’t replicate em in a much simpler manner.

1 Like

What do you mean detects player/root, Do you mean a ray that goes forward for a certain amount of studs ?

2 Likes

Ray that looks for nearest player that is in range of it

1 Like

You could iterate through every player and then check which of them got the lowest magnitude from the thing you want to check.

1 Like

Alright I understand what you need,

a ray is basically a part that extends from a certain point to another point that you specify
In your case you’ll need a part that for instance extends 10 studs infront of the player, how do i go about that? its quite simple, use LookVector and multiply it by the amount of studs you want

Example:

local ray = Ray.new(character.HumanoidRootPart.Position, character.HumanoidRootPart.CFrame.LookVector * 10)

local hit, position = workspace:FindPartOnRayWithIgnoreList(ray, {character})

if hit then
       if (character.HumanoidRootPart.Position - position).magnitude <= 10 then
             -- do what you need here
       end
end
1 Like

Also, Dont foget the checking, Since you need it to be a player
@Czlopek

If you dont know how you’ll check ill be happy to explain

But if im not mistaking this would force a ray to be only in front of the player. not look for nearest target or turn on to it.

If you want to look for the nearest target then i suggest you do what @AstralBlu_e suggested.

1 Like

Or you could fire multiple rays in different directions

If you don’t want something to go straight in one direction, you don’t want a ray.

2 Likes

here is the part of the code i was trying to simplefie

	local ray = Ray.new(myHead.Position,(target.Position - myHead.Position).Unit * 25)
	local hit,position = workspace:FindPartOnRayWithIgnoreList(ray,{script.Parent})
	if hit then
		if hit:IsDescendantOf(target.Parent) then
			return true
		end
	end
	return false
end
function findTarget()
	local dist = 100
	local target = nil
	for i,v in ipairs(workspace:GetChildren()) do
		local human = v:FindFirstChild("Humanoid")
		local torso = v:FindFirstChild("Torso") or v:FindFirstChild("HumanoidRootPart")
		local team = v:FindFirstChild("Team")
		if human and torso then
			if checkDist(torso) < dist and human.Health > 0 then
				if team then
					if team.Value ~= myTeam.Value then
						dist = checkDist(torso)
						target = torso
					end
				else
					dist = checkDist(torso)
					target = torso
				end
			end
		end
	end
	return target
end

it is using rays to do the looking, so I was expecting that rays are what I was looking for

Indeed there are no things to simply there (at my sight). Its the most simplified.

Rays literally create a ray that goes from a position to other, If there are any objects that the ray hits and that object isnt in the ignore list then it will return that object and the ray will end there. (Basically it wont go further when it hits a object thats not in the ignore list)

Which means it can be from a location to other.

2 Likes

oh … in that case i am just a dummie