Changing type of pathfinding based on sight?

Sorry for the vague topic with no tangible script yet, but this is simply a thought I had as I lay on my bed.
The main issue with MoveTo() when it comes to in game characters or entities, is that the entity must have a straight, unobstructed path to their target.

The main issue with Roblox’s pathfinding is that it is infinitely slow. This isn’t TOO bad of an issue when the entity is far away, but becomes ever more apparent when the entity is right up next to you. Walking around it in circles makes it’s life ever so hard.

I’m making a game with in-game zombies, and I had a shower thought that if a raycast between the zombie and the player is unobstructed (or in layman’s terms the zombie can see the player), the zombie could use MoveTo() considering how easy it’d be, and if the zombie’s raycast was obstructed (can’t see the player) then the zombie uses pathfinding to get to the nearest player. My main issue is that other moving objects like zombies may horribly bog down this system, but I think it’s worth a shot for an easy way to make a dynamic movement system for NPC’s, particularly those who want to get to the player.

What are your thoughts? Do you think this is feasible, or do you see numerous potential issues with it?

1 Like

Sure, seems like a good idea to me. Raycasts aren’t that expensive. I wouldn’t worry about it unless it becomes an issue.

You could have a hybrid approach.

Raycast for line of sight, and for ground immediately in front of the zombie. If we have both, use MoveTo() to inch closer—to the ground position we hit.

If you lose line of sight or you’re gonna go off a cliff, stop moving and wait for pathfinding results until you get line of sight and walkability again.

You can get fancy with it, and constantly be calculating paths even when you don’t need them, so that when you do need them, you can just grab the most recent one immediately.

For example:

We have line of sight (A), and ground in front of us (B)

    zombie                    player
      O          (A)            O
     /|\ ————————————————————> /|\
      |    |(B)                 |
     / \   V                   / \
     —————(X)——+          +——————————————
               |          |
               |          |
               |          |

Inch forward to (X):

         zombie               player
           O          (A)       O
          /|\ ———————————————> /|\
           |    |(B)            |
          / \   V              / \
     ——————————+          +——————————————
               |          |
               |          |
               |          |

Now stop and calculate a path (or grab the most recent path if you’ve been calculating them continuously) and use that until we’ve made it around this hole.

1 Like

I’m thinking that a cheap way to cover the whole idea of no walkability even if there is a line of sight is to simply put invisible pillars in places that zombies can’t walk. I’ll make it a certain CollisionGroup that only zombies register, but players don’t, so zombies know to go around it.

This way, if there is a hole in the ground, players can fall through and die, but zombies won’t (unless a player manages to push them to it which I don’t mind leaving as a mechanic), and will simply go around it.