• 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 asecretspot · Jan 25, 2014 at 11:52 PM · c#animation

Unsure of how to make a certain animation play

I am relatively new to scripting and I am in the process of learning C#. T$$anonymous$$s site has been helping me a lot with previously answered questions but unfortunately I can't find an answer to t$$anonymous$$s particular question w$$anonymous$$ch I am asking.

I want to know how to make a certain animation play with Unity's animator component. I have gotten running, jumping, and other animations to work fine. However, I am unsure how to play a "gettingHit" animation the right way. By that I mean, I want an animation to play when my player gets $$anonymous$$t by an enemy attack.

I'm not sure whether t$$anonymous$$s is a small problem like I have just been misplacing my code, or if I need to change some lines to get it to work. Below are the lines I have been trying to place into the script in various places and changing t$$anonymous$$ngs around trying to get it to work properly but I can't seem to figure it out.

 if (gettingHit) {
         gettingHit = false;
         animator.SetBool ("GettingHit", false);
 }
 if (*****notsurewhatgoeshere*****) {
         gettingHit = true;
         animator.SetBool ("GettingHit", true);

}

Here is what the rest of the script looks like: `using UnityEngine; using System.Collections;

public class PlayerHealth : MonoBehaviour { public float maxHealth = 100; public float curHealth = 100; public float healthbarlength; private bool dead; private Animator animator; private bool gettingHit;

 void Start () {
     animator = GetComponent<Animator>();
     healthbarlength = 250;
     }

     void Update () {

             if (dead) {
                     dead = false;
                     animator.SetBool ("Dead", false);
             }
             if (curHealth <= 0) {
                     dead = true;
                     animator.SetBool ("Dead", true);
             }
             
     }

 void OnGUI(){
     GUI.Box(new Rect(10, 10, healthbarlength, 20), curHealth + "/" + maxHealth); 
 }
     public void ApplyDamage ( float damage ){
         if (curHealth > 0){ // if enemy still alive (don't kick a dead dog!)
             curHealth -= damage; // apply the damage...
         }

     // <- enemy can emit some sound here with audio.Play();
             if (curHealth > maxHealth) { 
                 curHealth = maxHealth;
                 if (curHealth < 0){ // if health has gone...
                     curHealth = 0;
                     // enemy dead: destroy it, explode it etc.
                 }

                 }

     healthbarlength = 250 * (curHealth / (float)maxHealth);
 }

}`

Thanks for taking the time to view the problem I have been having. Oh and don't mind the "dead" animation that is just what I will be working on after I get the $$anonymous$$t animation working properly.

Comment
Add comment
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

1 Reply

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

Answer by superluigi · Jan 26, 2014 at 12:09 AM

I would have somet$$anonymous$$ng like

 function OnTriggerStay(other : Collider)
 {
   if(!invincible)
   {
      if(other.gameObject.tag == "Enemy")
      {
         gettingHit = true;
         animator.SetBool ("GettingHit", true);
         invincible = true;
         yield WaitForSeconds(1);
         invincible = false;
         animator.SetBool ("GettingHit", false);
      }
   }
 }
Comment
Add comment · Show 1 · 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 asecretspot · Jan 28, 2014 at 12:52 AM 0
Share

Thank you! your answer got me on the right track.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Unassigned Reference Exception 4 Answers

Animated Texture Offset 1 Answer

adding animation dynamically by script 0 Answers

Playing an animation with C#? 1 Answer

SendMessage not working While Animator is Enabled 0 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