Raycast returning in wrong place

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    A flying script where the npc stops if theres a wall infront of it
  2. What is the issue? Include screenshots / videos if possible!
    The raycast returns a part which is well out of its supposed range.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have sifted through lots of dev forum posts but i dont see any solutions
    My flying script:
local npc = script.Parent
local hrpOfNPC = npc:WaitForChild("HumanoidRootPart")
local humanoid = npc:WaitForChild("Humanoid")
local plrsHit = {}
local maxDistance = math.huge
local pursuitCooldown = 0
local RP = RaycastParams.new()
RP.FilterDescendantsInstances = {npc}
RP.FilterType = Enum.RaycastFilterType.Exclude

npc.Humanoid.Touched:Connect(function(touch)
	if humanoid.Health >= 1 then
		local player = game.Players:GetPlayerFromCharacter(touch.Parent)
		if player and not plrsHit[player] then
			plrsHit[player] = true
			touch.Parent.Humanoid:TakeDamage(8)
			wait(1)
			plrsHit[player] = false  
		end
	end
end)

local function getClosestPlayer()
	local plrs = game.Players:GetPlayers()
	local closestHRP
	local closestDistance = math.huge

	for _, plr in pairs(plrs) do
		if plr.Character and plr.Character:FindFirstChild("HumanoidRootPart") and plr.Character:FindFirstChild("Humanoid").Health > 0 then
			local hrp = plr.Character.HumanoidRootPart
			local distanceBetween = (hrpOfNPC.Position - hrp.Position).Magnitude

			if distanceBetween < closestDistance then
				closestHRP = hrp
				closestDistance = distanceBetween
			end
		end
	end
	return closestHRP
end

while wait(pursuitCooldown) do
	local targetHRP = getClosestPlayer()
	if targetHRP and (hrpOfNPC.Position - targetHRP.Position).Magnitude <= maxDistance then
		npc.Humanoid:MoveTo(targetHRP.Position)
		if targetHRP.Position.Y > hrpOfNPC.Position.Y then	
			local raycast = workspace:Raycast(hrpOfNPC.Position, Vector3.new(hrpOfNPC.Position.X, hrpOfNPC.Position.Y + 3, hrpOfNPC.Position.Z), RP)
			print(raycast.Instance)
			if not raycast.Instance then
				npc:MoveTo(hrpOfNPC.Position.X, hrpOfNPC.Position.Y + 1, hrpOfNPC.Position.Z)
			end
		end
		if targetHRP.Position.Y < hrpOfNPC.Position.Y then	
			local raycast = workspace:Raycast(hrpOfNPC.Position, Vector3.new(hrpOfNPC.Position.X, hrpOfNPC.Position.Y - 3, hrpOfNPC.Position.Z), RP)
			print(raycast.Instance)
			if not raycast.Instance then
				npc:MoveTo(hrpOfNPC.Position.X, hrpOfNPC.Position.Y - 1, hrpOfNPC.Position.Z)
			end
		end
		if targetHRP.Position.Z > hrpOfNPC.Position.Y then	
			local raycast = workspace:Raycast(hrpOfNPC.Position, Vector3.new(hrpOfNPC.Position.X, hrpOfNPC.Position.Y, hrpOfNPC.Position.Z + 3), RP)
			print(raycast.Instance)
			if not raycast.Instance then
				npc:MoveTo(hrpOfNPC.Position.X, hrpOfNPC.Position.Y, hrpOfNPC.Position.Z + 1)
			end
		end
		if targetHRP.Position.Z < hrpOfNPC.Position.Y then	
			local raycast = workspace:Raycast(hrpOfNPC.Position, Vector3.new(hrpOfNPC.Position.X, hrpOfNPC.Position.Y, hrpOfNPC.Position.Z - 3), RP)
			print(raycast.Instance)
			if not raycast.Instance then
				npc:MoveTo(hrpOfNPC.Position.X, hrpOfNPC.Position.Y ,hrpOfNPC.Position.Z - 1)
			end
		end
	end
end

humanoid.HealthChanged:Connect(function()
	if humanoid.Health <= 0 then
		wait(3)
		npc:Destroy()
	end
end)

2 Likes

i can only assume that the vector3s that you input into the workspace:Raycast method are maybe wrongly calculated. you could try spawning parts in those positions to maybe see if thats the issue. also, i wouldnt really recommend using model:MoveTo because its buggy and setting the primarypart cframe is overall better.

2 Likes

are you working on a cheese escape type game?

2 Likes

Its a 2D type battle game where you can fight enemies or do parkour

Direction (second argument of :Raycast) depends on Origin

the formula is Origin + Direction

so what your doing is adding up origin + (origin + direction)

it must be

Vector3.new(0, 0, 3)