How to i make AI that will not attack you if your in a hiding spot

Hello, I have recently made a game that uses an enemy AI. In my game, I have hiding places to hide from the enemy but it keeps trying to get me through the wall. For my game, this is kind of a big issue. I would love to know if you could help me figure out a way to fix it.

The script is based on the magnitude and if you are in range it will chase you. so I won’t stop chasing you if you are in range. this means that you can’t escape from the hiding place because you are trapped by AI.

1 Like

For my NPCs, I use raycast to see if the NPC can actually see the player before launching a ranged attack. So if you cast the ray towards the player, you need to check the returned part to see if it belongs to a humanoid.

-- Checks to make sure the target is in line of sight.
local function checkLineOfSight(vector)
	local vect = vector * 1.3
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Blacklist
	params.FilterDescendantsInstances = { bossModel }
	local result = game.Workspace:Raycast(bossRoot.Position, vect, params)
	if result == nil then
		return true
	else
		local part = result.Instance
		local char = part:FindFirstAncestorOfClass("Model")
		if char ~= nil then
			local human = char:FindFirstChild("Humanoid")
			if human ~= nil then
				return true
			end
		end
	end
	return false
end

You can get the vector from the position of the NPC and the targeted player like this:

vector = (target.root.Position - bossRoot.Position)

The bossRoot variable is the HumanoidRootPart of the NPC. The target.root is the HumanoidRootPart of the player. bossModel (in the function) is the Character model of the NPC. Of course, if the target is not in line of sight, then pick a different target.

3 Likes

A way to do this is by, changing a player’s value if they are in a hiding spot, and if that value is true then the enemy AI will not get the player

thank you, I will try this out!