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
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:?
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
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
}
}
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.