Phase II Progress Report — New enemy functionality — Backwards attack with raycasting
In this article, I will discuss the new backwards shooting feature I have implemented.
The enemy grunt has been given a new once-off attack. They can shoot a slow-moving laser backwards. To implement this I have used a raycast which, when colliding with the player, will trigger the attack.
Basics of the Raycast
The raycast in this scenario is taking a few parameters which I will explain.
The origin is simple the 2d vector point we wish the raycast to start from.
The direction is the 2d vector direction we wish our ray to be cast in.
The distance is the float on how far the ray will be cast
The layerMask is the layer on which we check for objects to collide with.
In this example, I have created a layer called “EnemyAttackLayer” and assigned the player to this layer in the inspector.
When the ray is cast the collision information is stored in a RaycastHit2D variable. We can then check the information stored to see if it was the player that was hit and run some functionality if it was. I then turn the _canShootBackwards bool to false so the enemy can not shoot backwards again.
Now to see this in action.
That’s all for now.