Howdy developers! I come to you today seeking assistance. I’ll try to keep the description as short as possible.
Monster exists in a labyrinth. He chases a player if he comes within line of sight. While not chasing, he travels node-to-node, turning randomly at each three-way intersection. Each ‘node’ is a part at the center of an intersection. Monster kills you by touching you.
The problem is that Monster can only travel node-to-node, so while chasing a player - when he gets to an intersection - he must choose which node to go to next. Right or left? The player is down one of them, and at the end of each hallway is another node at each intersection.
What methods would I use to determine which node Monster should travel to next, if the player exists somewhere between Monster and the left or right node? (There may be obstacles)
Just know that if you do that it won’t be real-time and will stutter the NPC however you could counter this by having it switch to :MoveTo() if the player sees it. To prevent it from hugging the walls change the agents size
To determine which node the monster should go to, I would say start with pathfinding to the node the player is closest to, if that isn’t fast enough (or just doesn’t work) then use an a-star algorithm to find the closest (primal) path to the player using just the nodes. (I say primal because it should only be used as a guide, not the exact path itself.)
local playerCFrame: CFrame = Player.CFrame
local monsterCFrame: CFrame = Monster.CFrame
local direction: Vector3 = playerCFrame.Position - monsterCFrame.Position
if monsterCFrame.RightVector:Dot(direction) > 0 then
print("Right")
else
print("Left")
end
Or else you can use a clever technique:
local playerCFrame: CFrame = Player.CFrame
local monsterCFrame: CFrame = Monster.CFrame
if monsterCFrame:ToObjectSpace(playerCFrame).X > 0 then
print("Right")
else
print("Left")
end