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 Darwin Mecharov · Apr 15, 2014 at 08:39 AM · animationjavascriptcrossfadevector3.distance

How to play an animation at least once before changing

Hello guys! I've got an enemy with attack and running animations, and will only do these at a certain range. Problem is that when I go between two given ranges too fast, the animations immediately change, making it unrealistic. I've tried using animation.CrossFade but it doesn't work much, so any ideas on how to fix this? Oh, and here's my script:

 var Player : Transform;
 var MoveSpeed = 1;
 var MinDist = 2;
 var MinDist2 = 4;
 var Unseen = 15;
 
 function Update ()
 {
     transform.LookAt(Player);
     if(Vector3.Distance(transform.position, Player.position) <= MinDist)
     {
         Debug.Log("Attacking");
         if(!animation.IsPlaying("Attack"))
             animation.CrossFade("Attack");    
     }
     if(Vector3.Distance(transform.position, Player.position) <= MinDist2)
     {
         if(Vector3.Distance(transform.position, Player.position) >= MinDist)
         {
             Debug.Log("Attacking2");
             if(!animation.IsPlaying("Attack2"))
                 animation.CrossFade("Attack2");
         }
     }
     if(Vector3.Distance(transform.position, Player.position) >= MinDist2)
     {
         if(Vector3.Distance(transform.position, Player.position) <= Unseen)
         {
             Debug.Log("Not in attack range");
             transform.position += transform.forward * MoveSpeed * Time.deltaTime;
             if(!animation.IsPlaying("Running"))
                 animation.CrossFade("Running");
         }
     }
     if(Vector3.Distance(transform.position, Player.position) >= Unseen)
     {
         Debug.Log("Not within sight");
         if(!animation.IsPlaying("Idle"))
             animation.CrossFade("Idle");
     }
     
 }

That's it. If possible, I want to be able to play the "Attack" animation at least once before switching animations. Thank you very much in advance, and have a good day!

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

2 Replies

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

Answer by deltamish · Apr 15, 2014 at 04:18 PM

Hi you set a higher CorssFade Time

 // I have edited your code a bit
 if(Vector3.Distance(transform.position, Player.position) <= MinDist2)
 {
     Debug.Log("Attacking2");
     if(!animation.IsPlaying("Attack2"))
      animation.CrossFade("Attack2",0.7f);
 
 }else if(Vector3.Distance(transform.position,   Player.position) <= MinDist)  // used else if to check if its greater than "MinDist2" value and less than "MinDist"
 {
    Debug.Log("Attacking");
    if(!animation.IsPlaying("Attack"))
    animation.CrossFade("Attack",0.6f);  // U could also try using CrossFadeQued
 }
 
 
 
Comment
Add comment · Show 2 · 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 Darwin Mecharov · Apr 16, 2014 at 02:00 AM 0
Share

I'll try that

avatar image Darwin Mecharov · Apr 16, 2014 at 11:43 AM 0
Share

Tested and is working properly with a few adjustments. Thanks!

avatar image
1

Answer by JCX · Apr 15, 2014 at 08:45 AM

use Animator, there you can visually set this up, Animation is deprecated anyway

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 Darwin Mecharov · Apr 15, 2014 at 08:48 AM 0
Share

Any other ways? Oh, and thanks for the quick reply!

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

How to make an animation finish after looping smoothly? 0 Answers

Animation Crossfade won't play but Animation Play does 1 Answer

Animation.CrossFade help please 3 Answers

what is difference of animation blend and animation crossfade? 0 Answers

Animation doesn't work 1 Answer


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