There‘s not only one solution to a problem to solve. There are many ways to solve problems.
Revisiting the 2D Space Shooter project. I was going through some tasks, and decided to work on implementing Enemy Shields.
Objective: Creating an Enemy Shield behavior in a creative and unique way to make it work through trial and error and a lot of testing.
To begin this process, I duplicated my Player Shield GameObject.
We will go into the Enemy Prefab and make our new enemy shield a child of the parent Enemy object.
Implementing the Enemy Shield behavior.
The Enemy Shield needs to have its own script attached.
Let’s break down what we’d need to create the Enemy Shield logic.
- A Collision Detection of OnTriggerEnter2D
- A health amount variable set globally
- Reference to the PlayerProjectile class script
- Component access to the Rigidbody2D
- The GameObject the script is attached to
Now lets see how I made this particular system work.
I was able to approach this scenario creatively, and in a way I did not know after testing that this was one way to implement this feature.
Additionally, we need to see if the other gameObject’s tag will be detected by a PlayerProjectile.
If we did that successfully, we need to get inheritance of the PlayerProjectile script.
From here we get the Rigidbody2D component and go into the properties:
- collisionDetectionMode = collisionDetectionMode.Continous (Ensures all collisions are detected when the Rigidbody2D moves)
- collisionDetectionMode = collisionDetectionMode. Discrete (Only collisions are detected when the Rigidbody2D is moved to a new position)
Using the discrete property, it worked perfectly for my implementation; And I’m sure there are many other ways to accomplish this same feature.