Search Unity

Question Knockback doesn't move gameObject

Discussion in 'Getting Started' started by Katamarang00, Feb 20, 2024.

  1. Katamarang00

    Katamarang00

    Joined:
    Jun 15, 2020
    Posts:
    2
    I'm trying to create a knockback system for some enemies in my game, I'm following this tutorial:

    But I am using the A* Grid graph pathfinding.
    Here's the code:
    Code (CSharp):
    1. public void playFeedback(GameObject sender, float strength, float delay)
    2.     {
    3.         StopAllCoroutines();
    4.         knockbackBegin?.Invoke();
    5.         Vector3 direcion = (transform.position - sender.transform.position).normalized;
    6.         print(direcion);
    7.         rb.AddForce(direcion * strength, ForceMode.Impulse);
    8.         print("Enemy Knockback");
    9.         StartCoroutine(knockbackReset(delay));
    10.     }
    11.  
    12.     private IEnumerator knockbackReset(float delay)
    13.     {
    14.         yield return new WaitForSeconds(delay);
    15.         knockbackEnd?.Invoke();
    16.         rb.velocity = Vector3.zero;
    17.     }
    The knockbackBegin and knockbackEnd disable the Pathfinding so they don't get in the way of the rigidbody. addForce. However, they still don't get moved backwards