Top Down Shooting Game (part 13)

Today, I tried to make the enemies push back, but it didn’t work. So, I made them stop for 1 second after getting shot and then resume moving.

In the Enemy Script

First, create two variables: stopTime and freezeDuration, both as floats. The stopTime will store the moment when the enemy stops, and freezeDuration will define how long the enemy stays frozen (in seconds).

Next, check if stopTime is greater than 0:

  • This means is the stopTime is greater than 0, the enemy is still paused, so decrease the stopTime by Time.deltaTime.

If stopTime is less than or equal to 0:

  • We reset stopTime to 0 to prevent it from negative.
  • This means the enemy is no longer frozen, and we can make it move again.

And then if it is less than or equal to 0, make it get target, and call the moveEnemy() function.

In the FixedUpdate() function, if the stoptime is less than or equal to 0, set the enemy’s velocity. Otherwise, set its velocity to 0.

Create a moveEnemy() function below the GetTarget() function. In this function, we just need to set the velocity of the enemy to make it move when called.

Finally, when the object collides with the enemy as “Bullet”, set the stopTime equal to the freezeTime.

And this is how my game work after run it.

Tags:

No Responses

Leave a Reply

Your email address will not be published. Required fields are marked *