[Solved] How to: Register a hit in 3D?

Hello All,

I have been playing around with an extremely small project where I have replaced the awful looking Ethan 3rd person controller with another more detailed character from the asset store. I’ve kept the same movements from Ethan because the blend tree makes the movements very good and smooth and because I basically have no idea how to make an animator that good :p.

So, I’ve added in a couple of animations into the animator that came with the character, one being an attack with a sword, but, I don’t have any idea on how I can register a hit with the sword (for the purpose of taking off a enemy health). Now I’ve done this many times on 2D games using triggers OnTriggerEnter2D or OnCollisionEnter2D and tags for the collisions, but, never in a 3D realm. And have no idea how to go about this!

Like, for instance, the sword is part of the character animation, would I need to make the sword an object with a collider around it to make a collision happen.

I am a bit lost lost with this so any help would be greatly appreciated.

Thankyou and Regards.

Did you try to use OnCollision on the sword?

Usually what I’d do in that case is a simple raycast from the player straight forward. If its RaycastHit is within the “sword range”, I apply the damage. I don’t think anyone is looking closely to ensure the sword hits the enemy. But I’ve also been spending 99.9% of my coding time on mobile games where I try to avoid using the physics engine, so perhaps OnCollision is better.

A raycast might be a good option or perhaps I should I say the “only” option. I applied a box collider to the sword for Unity to read a collision and for some reason it stuffed up the animator :face_with_spiral_eyes:. Now the controller is continually reading a crouch after the Idle animation has played even if I specify that the crouch boolean is set to false - even though that was fixable I don’t know what would happen if “Ethan” really needed to crouch :eyes:.

And I can’t seem to get the Sword Component itself to register a hit by using a “tag”. It seems using the object to read a collision is a bridge too far - at least for the time being.

Thanks again for taking the time to respond - much appreciated.

Try putting a trigger collider on the sword and use OnTriggerEnter. That way no physics will be involved.

1 Like

Aha! Now that does work :sunglasses:. I kept on thinking that the trigger wouldn’t work and was deliberately staying away from it - note to self never trust my instincts with Unity :p.

Now I’m one step closer, the only thing i need to do now, is to find a way to have the Trigger fire when the actual attack animation is being played by the Animator - I have an idea, it’s a longshot … but as they say it’s better than none.

Thankyou so much for your help, I’ve gotten closer to my goal than I ever imagined! This project is part of a trial for a bigger project.

The idea I had didn’t pan out.

I thought I could get the main animator component for the character as a public var in the sword object and get the bool value from the animator when the attack animation is set but Unity wouldn’t allow that.

Left me scratching my head! At the very least I have the basis for a new question for the forum :p.

Look here for how to tell what animation is playing: http://answers.unity3d.com/questions/362629/how-can-i-check-if-an-animation-is-being-played-or.html

That would be a simple if statement to fix your problem :slight_smile:

Also, in your controller, if you start attacking (let’s say you press the mouse button), you can enable a bool that would allow the OnTriggerEnter to run. Basically, just find a way to detect when it’s attacking.

http://answers.unity3d.com/questions/362629/how-can-i-check-if-an-animation-is-being-played-or.html[/QUOTE]
Now how on earth did you find that :p.

Ok, so, that works. The controller can now be used to tell which animation is being played.

Now please remember I am not that experienced with Unity, so, I am having trouble following what you are saying about using a bool.

By this do you mean setting up a bool value in the SwordCollider class if a hit is registerd.

And then combining this bool value with the Character Controller animator (ie attack Animation) and a function to access the bool in the SwordCollider to see if a “confirmed” hit has been made? Or are you espousing something different?

I am hoping its something different :slight_smile: As the above works to a certain extent - but, I can’t get Unity to register which actual “enemy” has been hit and take the health of the correct one!

And can I please have your real name so I can give you credit if I ever get the main game finished, you’ve given me all this help and encouragement to keep me going, so, it’s the least I could do - this game I am working has been one thing that really helps me going during the weeks martial arts lessons (instruction) - so, you have been a tremendous help. Do you have a Patreon account that I could look at?

Thankyou and Regards.

Bing. Lol.

No problem, we all start from somewhere! I was at a point when I knew nothing at all about development, and now I probably know about 2% of what there is to know about C# + Unity. I actually got into Unity because I accidentally downloaded the Editor instead of the Web Player. A bit of curiosity has kept me going a long way!

I mean that when the sword hits something, first check if it’s playing the attack animation, and then if it’s hitting an enemy.

Some pseudo-code to help you get the idea:

void OnTriggerEnter(Collider other)
    {
        //if playing attack animation and other object is an enemy
        if(this.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("Attacking") && other.tag == "Enemy")
        {
            //noob way
            other.GetComponent<HealthScript>().HP -= DamageAmount;
            //OR
            other.GetComponent<HealthScript>().TakeDamage(DamageAmount);
            //That would be better as then the enemy HealthScript would be able to detect that it took damage, and play a "Ouch" sound, check if health under 0, if so
            //play a death animation etc... I HIGHLY reccoment a TakeDamage method instead of directly changing the HP then checking often if the HP is under 0.
        }
    }

I’d prefer to stay as DroidifyDevs :slight_smile:

I’ve actually been thinking about changing my presence to Feline Interactive, but I’ll do that only if I publish a game. DroidifyDevs was something I thought up in less than 30 seconds and it sounds like a ripoff of Android. Anyways, I know the struggle is real. I’m also very busy; this month I’ve put in almost no time into my project (barely go on the forums, you see how late I am). I’m aiming to publish at least a public beta this year, but I’ll need to ramp up the dev time quite a bit. And for now, I don’t have a Patreon; I don’t have enough content for something like that. Thanks though.

D’oh … I’ve been going about it all wrong! If there’s a complicated indirect way to solve a problem then you can bet I’m there doing exactly that!

That pseudo-code looks suspiciously like actual code :smile:. I once lost marks on a assignment because my pseudo-code was actual code in C++, it was easier for me to do it that way.

The code I had looked somewhat like what you had, except, I didn’t realise I could use the “other” (or in my case col) to access the Health script on that object. It’s so simple, yet, so effective. I’ve printed out that pseudo-code with a note to put into my reference files, so, I don’t forget it in the future. I had a reference file to accessing scripts on other objects from a car game which I have taken a break from to work on this.

I can’t stress this enough you have been a huge help - many, many thanks :sunglasses:.

Ok, then, DroidifyDevs it is! I just wanted to make sure, I make a point of thanking those who have helped me as a sign of respect. If you were in the dojo I would bow to you for helping me - so please consider that a virtual bow.

Beir bua agus beannachct!

1 Like

Just an addendum to the above, I now have the code functioning:):p:smile:. I’ve put 2 enemies on the screen so you hit one of them subtract health, ten times and they fall over dead, and then move onto the other one and do the same! It’s great, it reminds me of my favourite game “For Honour” (w/ correct spelling:sunglasses:).

So, thankyou once again for all your help!

Warmest Regards.