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 Critcher · Nov 13, 2012 at 04:23 AM · animationattack

Animation freezing at first frame

Hi everyone,

I found this script on another thread a while ago and modified it first to return the monster to an idle state if out of range and then I tried to get it to fire an attack animation when enemy is close enough to player. Idle part and run part seem to work fine but attack locks on first frame until player moves out of range.

(the attack is a 35 frame animation if that has any value)

Here's the code, any ideas or leads would be great!!!

 var leader : Transform;
 var follower : Transform;
 var detectRange = 30.0;
 var attackRange = 2.0;
 var speed : float = 5;
 private var character: CharacterController;
 
 function Update(){
   if (!character) character = GetComponent(CharacterController);
   var dir = leader.position - follower.position;
   dir.y = 0;
   
   if (dir.magnitude <= attackRange){
   follower.forward = dir; 
   animation.Play("attack");}
   // This little feller right here seems to stop at frame 1, 
   // moving out of range restarts either idle or run without a problem
   // I thought it might be due to the speed that update cycles at but 
   // run and idle both work fine
   // replacing idle with attack has proved that attack animation works well 
   // (cause someone would ask about that I'm sure)
     
   if (dir.magnitude <= detectRange){
   follower.forward = dir; character.SimpleMove(speed*dir); 
   animation.Play("run");}
   if (dir.magnitude > detectRange){
   animation.Play("idle");}
 }
 

Your time is appreciated

Comment
Add comment · Show 1
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 svenskefan · Nov 13, 2012 at 08:27 AM 0
Share

Hi! You are constantly starting the animation(s) when the conditions are met; if (dir.magnitude <= detectRange){ follower.forward = dir; character.Simple$$anonymous$$ove(speed*dir); animation.Play("run");}

as long as (dir.magnitude <= detectRange) the script will start playing the animation. you need a way to tell if the animation is already playing, and only start it if it is not already playing.

1 Reply

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

Answer by Montraydavis · Nov 13, 2012 at 01:25 PM

It most likely has to do with the endless animation loop. Try

 animation.Play ("AnimationName") ;
 yield WaitForSeconds ( animations["AnimationName"].clip.length ) ;
 //...rest of code . . . .

Make sure to create another function. The above code should not go inside of Update, but instead, Update should call another function that contains this code. What it does is basically waits for the animation to end before replaying it.

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 Critcher · Nov 15, 2012 at 06:06 PM 0
Share

Thanks, moving it out to another function is the way to go!

avatar image Montraydavis · Nov 15, 2012 at 09:26 PM 0
Share

Yeah, Update and co-routines don't get along .

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

11 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

Related Questions

How to fix rig animation that stops short when increasing speed? 0 Answers

Can I make animations snap to a frame? 1 Answer

What is the best way to manage the attack and auto-attack animation per second when the object collides?,Best way to make 2 objects attack per time each other when colide? 0 Answers

How can I make my Axe in unity do damage to my Zombies 6 Answers

Play attack animation on mousebutton 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