- Home /
Enemy attacked state
I am trying to make some states for enemy while they are being attacked by the main player with its combo attacks .
So let's say when the sword collision occures , I play randomly my hit0/hit1 animation of the enemy soldier and so on till it has 0 HP and then I'll play the death anim . I want my soldier to change its "state"/animation to hit0/hit1 while being attacked .
Am I going in the right direction? or is this the best way to do it?
Thanks!
Do you mean have the enemy play an appropriate animation for the current attack in the combo?
Answer by ChrisSch · Feb 28, 2014 at 12:10 AM
I'll post this as an answer. Then I guess when you do your attack you can send a message to the enemy to play that animation, and that's it. :)
So if player is doing attack "fistAttack1" you send a message to the enemy script to play the animation called "fistAttack1Reaction" or something.
You can do that using the SendMessage() function or GetComponent(componentName) and then instructing the component you just got to activate the reaction.
Here's the unity reference links: :)
Send Message http://docs.unity3d.com/Documentation/ScriptReference/Component.SendMessage.html
GetComponent http://docs.unity3d.com/Documentation/ScriptReference/Component.GetComponent.html
EDIT:
Seeing as you're using collisions to detect attack, and because I'm not sure how your attack script is actually set up, and because it isn't as simple to get the name of the current animation playing, because there can be multiple animations playing at the same time on one object, I'll write this script using strings to see which attack is active. You could use booleans but then you'd need more variables and you'd have more enabling/disabling work. There's probably better ways of doing it, and also my script might have typos or bugs cause I cant test it atm. In any case you'll learn as you go. I commented a lot to explain best I can what's going on
PlayerScript
var currentAttack : String;
function Update()
{
if(Input.GetButtonDown("Attack1")
{
//Set the currentAttack variable to "Attack1"
currentAttack = "Attack1"
//using crossfade to transition the animations smoothly
//0.2 is the length of the animation transition (fade)
animation.CrossFade("Attack1", 0.2);
//The rest of your attack code goes here
}
else if(Input.GetButtonDown("Attack2")
{
currentAttack = "Attack2"
animation.CrossFade("Attack1", 0.2);
}
}
function OnCollisionEnter(coll : Collision)
{
if(coll.gameObject.tag == "Enemy") //check if its the enemy you hit
{
//check if you're using "Attack1"
if(currentAttack == "Attack1")
{
//Get the enemy script from the collision data
//We're calling your enemy script EnemyAI just for example
var enemyScript = coll.gameObject.GetComponent(EnemyAI);
enemyScript.attackReaction = "Attack1";
//That will tell the EnemyAI script
//that its being attacked by "Attack1"
}
else if(currentAttack == "Attack2") //or attack2
{
var enemyScript = coll.gameObject.GetComponent(EnemyAI);
enemyScript.attackReaction = "Attack2";
}
}
}
EnemyAI
var attackReaction : String;
function Update()
{
//Check if the attackReaction is "Attack1" or "Attack2"
//set by the PlayerScript with GetComponent
if(attackReaction == "Attack1")
{
//Play your animation called "Attack1Reaction" for example
animation.CrossFade("Attack1Reaction", 0.2);
if(!animation.isPlaying("Attack1Reaction")
{
//If its done playing set attackReaction to "None" or something, to end the loop.
//Otherwise itll probably continue repeating
//youll see, im just writing not testing because
//i dont have any animations to work with atm xD
attackReaction = "None";
}
}
else if(attackReaction == "Attack2") //or attack2
{
animation.CrossFade("Attack2Reaction", 0.2);
if(!animation.isPlaying("Attack2Reaction")
{
attackReaction = "None";
}
}
}
They both do different things but they can both access scripts as well. If you happen to be doing something they both can do, I think GetComponent is somewhat faster than Send$$anonymous$$essage. And yes that's pretty much how its done. xD
I can write up a quick script with GetComponent if you want, to see what i mean, and you adapt it to your script.
of course if you have time , that would awesome ))
I was asking if it is efficient because I also read from some practices that Send$$anonymous$$essage() is slower .
thank you
Posted as an edit to the answer! Hope it helps a bit. :)
$$anonymous$$ade another edit, you might wanna refresh if you saw it before I did. xD
Your answer
Follow this Question
Related Questions
Can the animation editor create local rotational data? 3 Answers
Adding animation clips via script 2 Answers
Can I make animations snap to a frame? 1 Answer
Adding Classes to a List of classes 1 Answer