The spider will fire a unique bullet that will be visually represented by a ball of spiderweb flying through the air. This bullet will freeze Hensen in place for a duration of 3 seconds when it collides with the PlayerController. A few changes needed to be made to the bullets and the PlayerController classes in order for this to be doable.

The first change I made was to make the bullet an abstract class. Previously, in the OnTriggerEnter2D method, I had been taking health from the entity but the spider's bullet does not take health so I decided to have an abstract method named OnCollision be called in place of doing that so I could create unique bullets that would do different things depending on how I implement that class. I feel like I REALLY should have had it this way to begin with, if I'm being honest.

To preserve the previous functionality, I wrote a DefaultBullet class that implements the abstract OnCollision method with the behavior that was present in the OnTriggerEnter2D method before.

I also went ahead and added a direction enum to the bullet rather than setting the move speed to -10 when I want it to move down like I had been doing before.
This led to a funny moment where my IDE tried to tell me it wasn't possible to apply the multiplication operator on operands of float and enum types to which I simply went "sure it is" before casting Direction to the float type which works, by the way, because enums are REALLY just a set of fancy integers. I did also manage to record this moment and you can see it here: https://youtube.com/shorts/8PTzmItL064

I also had to add a branch into the Update method of the PlayerController class in order to handle freezing the player's movement. After that was done, it was a simple matter of adding a public FreezeMovement method taking a float duration as a parameter that starts a helper coroutine that sets frozen to true and then back to false after waiting for (delay) seconds.

From here it was a simple matter of creating a new child class of Bullet that would call this FreezeMovement method OnCollision instead of damaging the entity. Something I love C# for, by the way, is pattern matching. Imagine if I had to check whether the otherEntity object had a PlayerController component and then GET that component? Honestly, I think I childed PlayerController to the Entity class SOLELY so I could do this in particular.













0 comments