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;
}
}