Issues with Ray Casting

Seen in this video I have a Character pathfinding to the player. One of the determining factors of whether or not the Character follows the player is whether it can see the player or not, this information is found using Ray Casting, the window on the left side of the screen is the server side and the output is saying that the character can see the player even though the player is behind a wall; I’ve tried many things to see if there was another issue in my code that was causing problems but I couldn’t find any.

local Raycast = workspace:Raycast(character.HumanoidRootPart.Position,player.Character.HumanoidRootPart.Position,RayParams)
			if Raycast then --Debug purposes
				print("Object blocking Ray: ".. Raycast.Instance.Name) --Debug purposes
			end
			if Raycast == nil or Raycast and Raycast.Instance.Parent == player.Character then
				print("player is seen")
				NearestPlayer = player
				destination = player.Character.PrimaryPart.Position
				NearestPlayerDistance = (player.Character.PrimaryPart.Position - character.PrimaryPart.Position).Magnitude
			end

The second parameter of workspace:Raycast is the direction of the raycast. If character is the enemy model, and player is you, then the code should be:

local origin = character.HumanoidRootPart.Position
local Raycast = workspace:Raycast(origin,player.Character.HumanoidRootPart.Position-origin,RayParams)
4 Likes

Give full script. We can’t help you without the rest of it.