Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

1
How do I code and attach a simple melee attack?
Post Flair (click to view more posts with a particular flair)
Post Body

I tried attaching this directly via Add Behavior onto the animation event, but it didn't work. Then I tried to attach it via an Animation Event and that didn't work either.

    public class SwordAttack : MonoBehaviour
    {


        void OnTriggerEnter(Collider other)
        {
            GameObject.Destroy(other);
        }
    }

It's supposed to just destroy the enemy on hit. I wanted to add more specific logic like death animations and knockback but that can wait until I understand how to do it.

Do I need to code it into when the actual attack is called in my main player script? I thought I could do I the way I tried above. Or is my code off?

I tried Googling a lot, but most of the tutorials start with adding a BoxCollider to represent the weapon and they use the OnCollision method instead. I was originally going to do that but I thought I could shortcut it (as in, the attack would only be active when I swung the sword, so I don't need to go to create a hitbox)

I also wanted to be able to write the method so that I could use it for both enemies and players, just that enemies would have 1 health (they just die) but the player would take damage.

So something simple for players like :

public void TakeDamage (int damage)
{
    currentLives--;

        // Check if lives are zero
        if (currentLives <= 0)
        {
            GameOver();
        }
}

Author
Account Strength
60%
Account Age
1 year
Verified Email
Yes
Verified Flair
No
Total Karma
2,362
Link Karma
1,945
Comment Karma
417
Profile updated: 1 day ago
Posts updated: 3 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
4 months ago