Jump to content

Check if path is blocked


Recommended Posts

Hi Guys

I'm working on a top down shooter.  I have AI that uses angleBetween to face the direction of enemies when they spawn.

The game uses a tilemap with some collisions randomly spawned around the map.

Is there a way that I can detect if there's a collision layer blocking the path between my AI and enemy sprites?

I don't want my AI to mindlessly fire their weapon if the enemy is behind a wall.

in my AI.js I have:

AI.Prototype.update = function() {
    this.rotation = game.physics.arcade.angleBetween(this, this.closestEnemy);

**Checks if enemy exists on map, uses Phaser weapon plugin**


Would I need to implement pathfinding to check if there's a direct path before firing or is there an easier way to go about this?

Link to comment
Share on other sites

I would suggest that the easiest way is for you to fire an 'invisible' bullet and check if it ever collides with anything. You can dramatically speed up this bullet as long as it does not travel so fast that it passes through the physics body without registering a collision.

I'm quite sure there are more performant ways like drawing a ray line and seeing if that gets intersected at any point between the enemy and the player. 

I hope this helps.

Link to comment
Share on other sites

Here is an example of the solution suggested by @megmut using raycasting with tilemaps and lines in Phaser:


In the example you are the player, but you could easily adapt it to your AI. Another way to check might be to add a sort of an invisble hazardous area/radar/lineofsight (arcade body) as a child of the AI, offset it and then check if the overlap includes a wall or not?


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...