Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question knockback on collision with enemy

Discussion in 'Scripting' started by SpyderManToo, Mar 8, 2021.

  1. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    in my 2d top down game, i want my player to experience knockback when they get in contact with an enemy but i cant seem to simulate it no matter what i do! heres the problem. i get hit the right amount then the next time i get hit i get hit twice the amount and it keeps on knocking me back more so i just deleted it
    can anyone show me how to add knockback? (im kinda stupid :p)

    heres movement code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movement : MonoBehaviour
    6. {
    7.     [Header("Movement and Rotation")]
    8.     public int moveSpeed;
    9.     Vector2 movement;
    10.     Vector2 mousePos;
    11.  
    12.     [Header("Knockback")]
    13.     public float kb;
    14.     public float kbStunTime;
    15.  
    16.     bool canMove;
    17.  
    18.     [Header("References")]
    19.     public Camera cam;
    20.     public Rigidbody2D playerRb;
    21.  
    22.     private void Start()
    23.     {
    24.         canMove = true;
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         movement.x = Input.GetAxisRaw("Horizontal");
    31.         movement.y = Input.GetAxisRaw("Vertical");
    32.  
    33.         mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
    34.     }
    35.  
    36.     private void FixedUpdate()
    37.     {
    38.         if (canMove)
    39.         {
    40.             playerRb.MovePosition(playerRb.position + (movement * moveSpeed * Time.fixedDeltaTime));
    41.         }
    42.  
    43.         Vector2 lookDir = mousePos - playerRb.position;
    44.         float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg;
    45.         playerRb.rotation = angle;
    46.     }
    47.  
    48.     private void OnCollisionEnter2D(Collision2D collision)
    49.     {
    50.         if(collision.gameObject.CompareTag("Enemy"))
    51.         {
    52.             //Vector2 dir = transform.position - collision.gameObject.transform.position;
    53.             canMove = false;
    54.             //playerRb.AddForce(dir * kb, ForceMode2D.Impulse);
    55.             StartCoroutine(KnockbackStunTime(kbStunTime));
    56.         }
    57.     }
    58.  
    59.     IEnumerator KnockbackStunTime(float cooldown)
    60.     {
    61.         yield return new WaitForSeconds(cooldown);
    62.         canMove = true;
    63.     }
    64. }
    65.  
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    It would help if you put in your original knockback code so we know how you approached it. Based on on that it should be simple to change it the way it should work.
     
  3. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    It should simply be a case of reversing the exact same code for your knockback force but having the code affect the player rather than the enemy when you collide with the enemy.
     
  4. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    i dont rly have knockback for when i hit my enemy right now. im talking about knockback to the PLAYER when you hit the ENEMY. ;-;

    i tried making a vector2 that calculates the direction and then using rigidbody.addforce but its rly unreliable
     
  5. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583


    It's also a bit unnecessary for you to use a coroutine for something as simple as a timer, set the float to the original time you want and use -= Time.DeltaTime to count down instead.
     
  6. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    ok thanks i will check out the video
     
  7. YungMarkyMark

    YungMarkyMark

    Joined:
    Feb 16, 2023
    Posts:
    1
    How would this be done in Unreal Blueprint??
     
  8. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,360
    You should ask this in the Unreal Engine forum or some other forum where people talk about Unreal Engine.