- Home /
Making enemy health drop if players sword hits enemy
Alright guys, title basically explains it all. How would I make it so that if the players sword hits and enemy, the health drops. I have a combo system set-up with mouse clicks, click once attack plays, click twice within a certain amount of time attack2 plays, and click three times within a certain amount of time, combo plays. How would I make it so that each time the sword swings the enemies life drops and more damage per attack combo? I'll be going to bed here shortly so i'll be on tomorrow if anyone needs me to post codes/scripts. Thanks a lot!
If you have colliders attached to the Sword and the Enemy, check if they intersect at any point in time and then depending on the Type of combo attack currently being used just reduce their Health by whatever modifier you want applied to that Attack.
The sword and the character are the same, they're not two separate pieces, the sword goes behind the character model, how would I make it so the collider is with the sword when he draws it and then goes with the sword when the sword goes behind the character model?
Answer by FuzzyLogic · Feb 17, 2014 at 06:43 AM
Use a sphere collider for weapon range, if the enemy is within weapon range and the player attacks, then decrement the enemy health
The sphere collider doesn't work for the sword length, it grows proportionally so every time I grow it, it just expands towards the body
The sphere collider is not for the weapon itself, it is for your swinging range (length of the arm plus the weapon). Center the sphere collider on your character and make it large enough to simulate your character spinning in a circle swinging the sword. So you would have a sphere that surrounds your character.
What I forgot to mention is when you detect an enemy collision with your sphere, then you need to check that the enemy is in front of your character and not behind, since the sphere will wrap around, it will collide with things that are behind you and you don't want to be hitting stuff behind you when you are swinging forward.
Ok, I made the sphere collider and now i'm trying to do the code part. I'm a beginner so i'm having a little trouble. Every time I swing the sword (Input.Get$$anonymous$$ouseDown(0)) it does not subtract the health. But when I remove that line, every time I walk into the enemy, it subtracts the health. This is what I have.
`void OnTriggerEnter(Collider other) {
float distance = Vector3.Distance (target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot (dir, transform.forward);
if (Input.Get$$anonymous$$ouseButtonDown(0)) {
EnemyHealth eh = (EnemyHealth)target.GetComponent ("EnemyHealth");
// animation.wrap$$anonymous$$ode= Wrap$$anonymous$$ode.Once;
// animation.CrossFade("Attack");
eh.AddjustCurrentEnemyHealth (-10);
}
}`
Your answer
Follow this Question
Related Questions
How to have BulletPrefab do the damage in instead of RayCastHit 0 Answers
Huge Framerate Drop 0 Answers
Is the v2019 Editor clamping the framerate to 60 fps in Game mode? 0 Answers
Metro 2033 / Outlast-like HUD? 0 Answers
how to destroy enemy 1 Answer