I creating a 2d platform game. I would like to bounce the player away after hit by the enemy, so the user get another feedback beside the sound effect.

How can I achieve my wish?

Use the following code for my enemy attack:

public float timeBetweenAttacks = 0.5f;     // The time in seconds between each attack.
    public int attackDamage = 25;               // The amount of health taken away per attack.

    Animator anim;                              // Reference to the animator component.
    GameObject player;                          // Reference to the player GameObject.
    PlayerHealth playerHealth;                  // Reference to the player's health.

    bool playerInRange;                         // Whether player is within the trigger collider and can be attacked.
    float timer;                                // Timer for counting up to the next attack.

    void Awake () {
        // Setting up the references.
        player = GameObject.FindGameObjectWithTag ("Player");
        playerHealth = player.GetComponent <PlayerHealth> ();
        //enemyHealth = GetComponent<EnemyHealth>();
        anim = GetComponent <Animator> ();

    void OnCollisionEnter2D (Collision2D other) {
        // If the entering collider is the player...
        if(other.gameObject == player) {
            // ... the player is in range.
            playerInRange = true;


    void OnCollisionExit2D(Collision2D other) {
        // If the exiting collider is the player...
        if(other.gameObject == player) {
            // ... the player is no longer in range.
            playerInRange = false;

    void Update () {
        // Add the time since Update was last called to the timer.
        timer += Time.deltaTime;

        // If the timer exceeds the time between attacks, the player is in range and this enemy is alive...
        if(timer >= timeBetweenAttacks && playerInRange) {
            // ... attack.
            Attack ();

        // If the player has zero or less health...
        if(playerHealth.currentHealth <= 0) {
            // ... tell the animator the player is dead.
            anim.SetTrigger ("PlayerDead");


    void Attack () {
        // Reset the timer.
        timer = 0f;

        // If the player has health to lose...
        if(playerHealth.currentHealth > 0) {
            // ... damage the player.
            playerHealth.TakeDamage (attackDamage);

The enemies do have a box collider 2D, and the script you can see above. Player has also a box and circle collider 2d, a rigid body2D and a player health script:

public int startingHealth = 100;                            // The amount of health the player starts the game with.
    public int currentHealth;                                   // The current health the player has.
    public Slider healthSlider;                                 // Reference to the UI's health bar.
    public Image damageImage;                                   // Reference to an image to flash on the screen on being hurt.
    public AudioClip hurtClip;
    private AudioSource source;
    public float flashSpeed = 5f;                               // The speed the damageImage will fade at.
    public Color flashColour = new Color(1f, 0f, 0f, 0.1f);     // The colour the damageImage is set to, to flash.

    public float internalStrength = 1000;
    public int externalStrength = 1000;

    Animator anim;                                              // Reference to the Animator component.
    PlayerMovement playerMovement;                              // Reference to the player's movement.

    bool isDead;                                                // Whether the player is dead.
    bool damaged;                                               // True when the player gets damaged.

    void Awake () {
        // Setting up the references.
        anim = GetComponent <Animator> ();
        source = GetComponent <AudioSource> ();
        playerMovement = GetComponent <PlayerMovement> ();

        // Set the initial health of the player.
        currentHealth = startingHealth;

    void Update () {
        //internalStrength -= Time.deltaTime;
        //externalStrength = Mathf.RoundToInt(internalStrength) + Random.Range(-100, 100);

        // If the player has just been damaged...
        if(damaged) {
            // ... set the colour of the damageImage to the flash colour.
            damageImage.color = flashColour;

        // Otherwise...
        else {
            // ... transition the colour back to clear.
            damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);

        // Reset the damaged flag.
        damaged = false;


    public void TakeDamage (int amount) {
        // Set the damaged flag so the screen will flash.
        damaged = true;

        // Reduce the current health by the damage amount.
        currentHealth -= amount;

        // Set the health bar's value to the current health.
        healthSlider.value = currentHealth;

        // Play the hurt sound effect.
        source.PlayOneShot (hurtClip);

        // If the player has lost all it's health and the death flag hasn't been set yet...
        if(currentHealth <= 0 && !isDead) {
            // ... it should die.
            Death ();

    void Death () {
        // Set the death flag so this function won't be called again.
        isDead = true;

        // Tell the animator that the player is dead.
        anim.SetTrigger ("Die");

        // Set the audiosource to play the death clip and play it (this will stop the hurt sound from playing).

        // Turn off the movement and shooting scripts.
        playerMovement.enabled = false;

If there is something unclear or whatever, ask me! Look out for some advice.

  • \$\begingroup\$ What have you tried so far? What are your problems that you've run into implementing this? Is there a reason you can't just apply a force to the player on collision with enemies? Also that's a lot of code your pasting into here - health system, variables... try trimming it down to just what's important. \$\endgroup\$
    – Superdoggy
    Commented Apr 4, 2015 at 17:05

2 Answers 2


you can try Rigidbody2D.AddForce to the palyer rigidbody, using force mode : Impulse ("This mode is useful for applying forces that happen instantly, such as forces from explosions or collisions").

Put tha addforce into your TakeDamage()


You could use RigidBody2D.AddForce(Vector3) to make knockback or you could have and animation of the player being bounced.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .