Raycasting towards a player

Alright so I’m having trouble. I can’t seem to raycast towards a player. I’m having trouble with this sample.

function canSeeILV(selfchar,lookfor,origin,direction)
	local ray = Ray.new(origin,direction)
	local partcell, v3pos = workspace:FindPartOnRay(ray,selfchar)
	if partcell then
		if partcell:IsDescendantOf(lookfor) then
			return true
		else 
			return false
		end
	end
	return false
end

The direction variable is the position of the target’s HumanoidRootPart.
It’s called in a while wait() do loop so it should update the position. But all it seems to do is cast into a weird direction where I have to walk to the model’s corner to trigger it.

The direction argument is not where it ends, it’s where it’s going to. There’s a clear difference. Here’s a visual example:

Direction

The direction is for where the ray travels, so if you add the origin point with the direction, that’s where the ray ends.

To solve the issue, just subtract the origin position with the target position and you would get the direction:

local Direction = origin - direction
local ray = Ray.new(origin,Direction)

I think you messed up your Direction thing.
It worked when it was

local Direction = direction - origin

not

local Direction = origin - direction 
2 Likes

Yeah he did to get a Vector From A to B, you have to do B-A, so from Origin to Target, you would have to do Target-Origin

1 Like