- Home /
Standart Enemy Melee
I have a model of a monster (enemy), and It have a "attack" animation. I have programmed the enemy to run towards the player when close, but how to make these steps:?
- Play animation "attack" when player is very close
- Minus Players HP by one
Answer by JesusChristChangedMe · Oct 31, 2010 at 10:25 PM
you have the hard part done. so when the monster hits you then you should have your player deduct a life. todo that your going to have to use static variables. make a new script called health control and use a switch statement and whatever number of hits you allow your character to take from the monster all the way down to case:0. and you should have in the script where it says to play your animation and you player getting hit healthcontrol.HEALTH -= 1; so that takes away a life from your character and tells the health control script to exacute the first case.
so your health control would look something like this.
static var HEALTH = 3;
function Update()
{
switch(HEALTH)
{
case 3:
//whatever you want to happen before your hit goes here
break;
case 2:
//whatever you want to happen after your hit once goes here
break;
and so forth like that.
there is a really good torandotwins tutorial about health control. here is a link
you could also look for the youtube tutorials made by canned mushrooms.
Uhm, Thats not really what I had in $$anonymous$$d. I need help with playing animation when the monster collides? with the player, and then a script to the player, that detects when enemy is colliding while playing the "attack" animation.
Answer by JesusChristChangedMe · Nov 02, 2010 at 01:53 AM
persona kinda had it right but this is what i would use. ok now i know what you want.
make sure this is attached to your monster. you want when the monster hits you to play the attack animation. Just make sure you tag the player "player"
function OnCollisionEnter(hit:Collider) { if(hit.gameObject.tag == "player") { animation.Play("attack");//or whatever your animation name is //here goes what you want to do to your player's health
} }
Answer by Persona · Nov 02, 2010 at 01:39 AM
Umhn....I'll give it a try. This is a rough idea on how to do it since I'm not on Unity right now and some coding will be off but basically:
function OnCollisionEnter(){ if(other.gameObject.CompareTag("Player")){
Attack(); } }
function Attack(){ play animation; other.sendMessage("Hurt")
}
Basically if you hit something that's tagged player, it runs the attack sequence. That plays the animation and sends a message to the player called Hurt, which should be a function like Attack, that reduced the health by one(Health--). The animation is merely a formality, the damage is done regardless of connectivity.
This is a rough idea of how to do it. The coding isn't completely accurate as I'm going off the top of my head.
Your answer
Follow this Question
Related Questions
Player Damage to enemy damage melee system 0 Answers
Complete AI Tutorial? 1 Answer
2D Random AI 1 Answer
free roaming enemy ai 1 Answer
Need help with knockback in C# Please! 2 Answers