Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by oleg9419 · Feb 27, 2014 at 07:02 PM · animationstate saving

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!

alt text

alt text

hit1.jpg (329.4 kB)
hit0.jpg (281.2 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ChrisSch · Feb 27, 2014 at 11:13 PM 0
Share

Do you mean have the enemy play an appropriate animation for the current attack in the combo?

avatar image oleg9419 · Feb 27, 2014 at 11:57 PM 0
Share

yes that's exactly what I meant :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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";
         }
     }
 }
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image oleg9419 · Feb 28, 2014 at 12:29 AM 0
Share

is this really the most efficient way ?

Thank you ! :)

avatar image ChrisSch · Feb 28, 2014 at 12:40 AM 0
Share

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.

avatar image oleg9419 · Feb 28, 2014 at 12:46 AM 0
Share

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

avatar image ChrisSch · Feb 28, 2014 at 01:01 AM 0
Share

Posted as an edit to the answer! Hope it helps a bit. :)

avatar image ChrisSch · Feb 28, 2014 at 01:04 AM 0
Share

$$anonymous$$ade another edit, you might wanna refresh if you saw it before I did. xD

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How to select an animation clip by index number? 7 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges