Jump to content
This site uses cookies! Learn More

This site uses cookies!

armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

  • 0
giltine528

Melee system that uses triggers?

Question

Hello fellow unity members, its been a while since ive worked on something and now i came back and wanted to create couple of things. One of them being the melee system that uses "hit on touch" method or how do you say it.

So what I want, Is whenever player swing his sword(plays animation) and if it touches the enemy, it damages it!

I used OnTriggerEnter method

Please login or register to see this code.

It works fine, but there couple of problems

1.(0:00) If the sword is inside an enemy and you press attack, the enemy wont receive damage, because enemy was already in the trigger and didnt enter it.

2.(0:46) If you swing your sword randomly during the animation, the damage is applied multiple times, since it enters the trigger multiple times.

 

Any idea on how to fix it? Please its really important, because if i get this problem solved, I might have my motivation back up on making a real project for once.

 

Thanks!

Share this post


Link to post
Share on other sites

4 answers to this question

Recommended Posts

  • 0

    public bool once = false;

    void OnTriggerStay(Collider other)
    {

        if (once == false)
        {

            once = true;
            ApplyDamage();

        }

    }

    void OnTriggerEnter(Collider other)
    {

        if (once == false)
        {

            once = true;
            ApplyDamage();

        }

    }

    void OnTriggerExit(Collider other)
    {

        once = false;

    }

    void ApplyDamage()
    {

        if (other.tag == "Enemy" && isAttacking)
        {

            Shake(0.2f);
            other.gameObject.GetComponent<EnemyHealthManager>().HurtEnemy(damage);

        }

    }

 

Should solve both problems.

Edited by gomnosano
Oops my bad... Forgot a very important part...

Share this post


Link to post
Share on other sites

  • 0
Please login or register to see this quote.

Okey Ive solved it with your idea by using Enter and Stay, but your method only is viable on one enemy, because once your hit one enemy, it enables the bool "once" and it cant damage the other enemies until the attack animation ends. I dont know if my approach was great but what I did is every time you hit someone, you Add that enemy to the list, and when another OnTriggerEnter event during attack animation occurs, it checks if the enemy is already in the list, if not apply the damage if yes, dont. 

Share this post


Link to post
Share on other sites
  • 0

I think you can just do this

void ApplyDamage()
    {

        if (other.tag == "Enemy" && isAttacking)
        {

            Shake(0.2f);
            other.gameObject.GetComponent<EnemyHealthManager>().HurtEnemy(damage);

            once = false

        }

    }

 

Instead of OnTriggerExit but you might have the same problem with the damage being applied multiple times on one swing.

Edited by gomnosano

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...