For now, our projectile just destroys itself on collision. But the goal is to fix those aggressive broken robots with it.
The first step is to write a function in the Enemy script to fix the robot and handle how the robots react when they are fixed.
Add a bool variable called broken and initialize it to true, so that our robot starts broken.
At the beginning of the Update function, add a test to see if the robot is not broken, and exit the function if it’s the case (when fixed, our robot will stop moving, so exiting the update function early will stop the code that moves them to be executed):
We just set broken to false and the simulated property of the rigidbody2d to false. This removes the Rigidbody from the physics system simulation, so it won’t be taken into account by the system for collision, and the fixed robot won’t stop the projectile anymore or be able to hurt the main character.
Now all left to do is to modify our OnCollisionEnter2D function from our Projectile script. We try to get an EnemyController from the object the projectile collided with, and if the object get one, we fix that enemy.
Note that we also removed the Debug.log too because we don’t need it anymore.
Now our projectile will fix the robot who will stop moving. You can then go against them and won’t be damaged.