Sawyer Posted April 7, 2017 Share Posted April 7, 2017 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** this.pistol.fire(); }; 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 More sharing options...
megmut Posted April 7, 2017 Share Posted April 7, 2017 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. Sawyer 1 Link to comment Share on other sites More sharing options...
samid737 Posted April 7, 2017 Share Posted April 7, 2017 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? Sawyer and MeMyMo 2 Link to comment Share on other sites More sharing options...
Sawyer Posted April 7, 2017 Author Share Posted April 7, 2017 Thanks guys That raycasting example worked perfectly @samid737 samid737 1 Link to comment Share on other sites More sharing options...
Recommended Posts