R
R
Rodrigez3212021-05-26 15:49:37
Unity
Rodrigez321, 2021-05-26 15:49:37

Why does damage stop passing through the enemy?

When attacking again, the enemy does not take damage until you move away from him.
I am using Unity 2D.

Here are my scripts.

Enemy script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    [SerializeField]
    int live = 3;

    Animator anim;
   

    bool hertlow = false;
    void Start()
    {
        
        anim = GetComponent<Animator>();
      
    }

    // Update is called once per frame
    void Update()
    {
        if (hertlow == false)
        {
            Damage();
        }
    }


    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.name == "AttackHitBox")
        {

            live--;
            StartCoroutine(ResAttack());
            anim.Play("HurtAnim");
        }
    }

    void Damage()
    {
        anim.Play("Normal");
       
    }
    
    IEnumerator ResAttack()

    {
        hertlow = true;
        yield return new WaitForSeconds(0.4f);
        hertlow = false;
    }
}

Character script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hero : MonoBehaviour
{
    

    [SerializeField] private float speed = 3f;
    [SerializeField] private int lives = 3;
    [SerializeField] private float jumpForce;
    [SerializeField] GameObject AttackHitBox;




    bool isGroundet = false;
    bool isAttacing = false;
    bool resDamage = true;
    bool atackAktive = false;

    [SerializeField]
    Transform groundCheck;



    private Rigidbody2D rb;
    private SpriteRenderer sprite;
    private Animator animator;
    public int power = 1;
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        sprite = GetComponentInChildren<SpriteRenderer>();
        animator = GetComponent<Animator>();
        AttackHitBox.SetActive(false);
    }

    private void Run()
    {
        if (isGroundet == true)
        {
            if (isAttacing == false)
                State = States.Run;
        }
        Vector3 dir = transform.right * Input.GetAxis("Horizontal");
        transform.position = Vector3.MoveTowards(transform.position, transform.position + dir, speed * Time.deltaTime);
        if (dir.x < 0.0f)
        {
            transform.localScale = new Vector3(1, 1, 1);

        }
        else
        {
            transform.localScale = new Vector3(-1, 1, 1);
        }
    }

    private void Jump()

    {

        rb.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);

    }


    private void Update()
    {
        if (isGroundet)
        {
            if (isAttacing == false)
                State = States.Idle;
        }

        if (Input.GetButton("Horizontal"))
        {
            Run();

        }

        if (Input.GetButtonDown("Jump") && isGroundet == true)
        {
            Jump();

        }

        if (Input.GetButtonDown("Fire1"))
        {
            if (atackAktive == false)
            {
                State = States.Attack;
                DoAttack();
                
            }

        }

    }



    private void FixedUpdate()
    {
        CheckGround();

    }

    private void CheckGround()

    {
        if (Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground")))
        {
            isGroundet = true;
        }
        else
        {
            isGroundet = false;

            if (isAttacing == false)
                State = States.Jump;
        }



    }

    private void DoAttack()
    {

        if (isAttacing == false)
        {

            isAttacing = true;
            StartCoroutine(DoAttacking());
            StartCoroutine(AttakSpeed());

        }
    }

    IEnumerator DoAttacking()

    {
        AttackHitBox.SetActive(true);

        yield return new WaitForSeconds(0.3f);
        AttackHitBox.SetActive(false);

        isAttacing = false;
    }

    IEnumerator AttakSpeed()

    {
        atackAktive = true;
        yield return new WaitForSeconds(1f);
        atackAktive = false;
        

    }


    private States State
    {
        get { return (States)animator.GetInteger("State"); }
        set { animator.SetInteger("State", (int)value); }
    }




}

public enum State
{
    Idle,
    Run,
    Jump,
    Attack
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
i__egor, 2021-05-27
@i__egor

You yourself wrote that the damage passes only when you enter the trigger (life--; - where you have it). ResAttack() - are you trying to make a delay? One OnTreggerEnter is indispensable here. For example, start an infinite caroutine with Enter and stop it with an Exit trigger.

IEnumerator ResAttack()
{
while(true)
{
        yield return new WaitForSeconds(0.4f);
        Damage();
}
}

life--; - more related to the Damage() function, and start the animation there

R
Rodrigez321, 2021-05-28
@Rodrigez321

I understand it should look like this?
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.name == "AttackHitBox")
{
live--;
StartCoroutine(ResAttack());
anim.Play("HurtAnim");
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.name == "AttackHitBox")
{
StopCoroutine(ResAttack());
}
}
private void Damage()
{
anim.Play("HurtAnim");
live--;
}
IEnumerator ResAttack()
{
while (true)
{
yield return new WaitForSeconds(0.4f);
Damage();
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question