• 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 ImpactUK · Nov 30, 2010 at 08:36 PM · animationupdatefunctionstop

Stop the Update function so death animation can complete.

Hello, i have an enemy character that has a death animation. When i kill the enemy, the death animation begins to play (In the "Death" function), stops halfway and repeats function Update and begins the chase animation again. How do i fix this?

var target : Transform; //the enemy's target var moveSpeed = 3; //move speed var rotationSpeed = 3; //speed of turning var attackThreshold = 3; // distance within which to attack var chaseThreshold = 10; // distance within which to start chasing var giveUpThreshold = 20; // distance beyond which AI gives up var attackRepeatTime = 1; // delay between attacks when within range

var maximumHitPoints = 5.0; var hitPoints = 5.0;

private var chasing = false; private var attackTime : float;

var myTransform : Transform; //current transform data of this enemy

function Awake() { myTransform = transform; //cache transform data for easy access/preformance }

function Start() { target = GameObject.FindWithTag("Player").transform; //target the player }

function Update () {

 // check distance to target every frame:
 var distance = (target.position - myTransform.position).magnitude;

 if (chasing) {
 Debug.Log ("player is close");
     animation.CrossFade("Walk");

     //rotate to look at the player
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);

     //move towards the player
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;

     // give up, if too far away from target:
     if (distance > giveUpThreshold) {
         chasing = false;

     }

     // attack, if close enough, and if time is OK:
     if (distance < attackThreshold && Time.time > attackTime) {
         animation.CrossFade("Attack");
         target.SendMessage( "ApplyDamage", 1);
         attackTime = Time.time + attackRepeatTime;
     }

 } else {
     // not currently chasing.

Debug.Log("not close yet " + distance); animation.CrossFade("Idle"); // start chasing if target comes close enough if (distance < chaseThreshold) { chasing = true; } } }

function OnCollisionEnter (collision : Collision) {

if(collision.gameObject.name==("SmallProjectile1(Clone)"))

{

var contact : ContactPoint = collision.contacts[0]; var rotation = Quaternion.FromToRotation(Vector3.up, contact.normal);

Hit(); }

}

function Hit () { // Apply damage hitPoints -= 1;

     if (hitPoints &lt; 0.0) {
     Die();

} }

function Die(){ animation.Play("Death"); chasing = false; }

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
1
Best Answer

Answer by Proclyon · Nov 30, 2010 at 09:08 PM

function Update()
{
   if(notPlayingDyingAnimation)
   {
      //Your normal update
   }
   else
   {
      //Your death animation code
   }
}
Comment
Add comment · Show 4 · 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 ImpactUK · Nov 30, 2010 at 09:30 PM 0
Share

Can't seem to get this to work correctly, it gives me semicolon errors for "void Update()" $$anonymous$$aybe i'm implementing it wrong?

avatar image Eric5h5 · Nov 30, 2010 at 09:32 PM 0
Share

"void Update" is C#; replace "void" with "function". It's generally best to post using the language that the question was asked in.

avatar image Proclyon · Dec 01, 2010 at 01:10 PM 0
Share

you are right, changed it in the edit.

avatar image ImpactUK · Dec 02, 2010 at 12:49 PM 0
Share

$$anonymous$$anaged to get it working now, thanks a lot for your help!

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

No one has followed this question yet.

Related Questions

How do i make my attack function not work whilst animation playing? 1 Answer

call a function at a specific frame of animation using mecanin 2 Answers

How do you check what function your animation is calling? 0 Answers

Can I make animations snap to a frame? 1 Answer

Having code fire once in update function? 2 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