- Home /
 
 
               Question by 
               RgClube · Oct 07, 2020 at 05:01 PM · 
                rigidbody2daddforce  
              
 
              Can't add AddForce or velocity to RigidBody2D
Hello everyone, I'm trying to get the player knockback when touching an enemy. Everything works fine, but when I try to apply force to the player nothing happens. Does anyone know what the problem might be? Here is the code:
 private void Start() 
   {
       Debug.Log(currentHealth);
       rb = GetComponent<Rigidbody2D>();
       playerMovement = GetComponent<PlayerMovement>();
       currentHealth = health;
       player = GameObject.Find("Player");
   }
   
  
   private void Update() 
  {
 
      if(rb.velocity.y < 0) {
          CheckAttackHitBox();
      }
      Debug.Log(currentHealth);
      if(rb.velocity.y >= 0 && playerStompedEnemy == false)
      {
           PlayerTouchEnemy();
      }
     
      if(touchDamageTime <= 0)
      {
          knockBack = false;
      }
      if(stompTime <=0)
      {
          playerStompedEnemy = false;
      }
  }
  
 private void PlayerTouchEnemy()
  {
      enemyDetected = Physics2D.OverlapBox(playerPos.position, sizeOfBox, angle, whatIsDamageable);
      touchDamageTime -=Time.deltaTime;
      if(enemyDetected != null && touchDamageTime <= 0 )
      {
          
          attackDetails.damageAmount = attack1Damage;
          attackDetails.stunDamageAmount = stunAmount;
          gameObject.transform.SendMessage("Damage", attackDetails);
          rb.AddForce(knockBackDirection);
          knockBack = true;
          touchDamageTime = 1.5f;
          
      }
  }
 
 
              
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by Lazy_Evaluation · Oct 07, 2020 at 08:43 PM
Try to use rb.AddForce(knockBackDirection, ForceMode.Impulse); or rb.AddForce(knockBackDirection, ForceMode.Acceleration); and increase the force.
Your answer