Tag: RaycastHit2D

  • Target scanners are essential for any game where you have an enemy mob that actively looks for prey. One way to scan for targets is to cast a circular physics ray based on the radius area you would like to scan.

    Below is a quick and helpful intro I’ve put together for anyone who needs a quick and easy target scanner to notify another script via event subscription. It searches for targets based on the tags set through the Unity Inspector.

    https://gist.github.com/MrChrisHammond/bc67005f5d54a9a473fa8c7c869a393a

    How to use from another script:

    https://gist.github.com/anonymous/29f7171737db1cb7f4e1034fa1e45ec1

     

    Final notes:

    This is a simple solution if you only require a few dozen scanners running at a time or less. If you have many more running concurrently then using tags may be slower and I would recommend scanning based on Unity Layers. You can set which layer to RayCast2D on or alternatively, you can set in Project Settings -> Physics 2D and set which physics layers interact with one another.