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 camtronius · Sep 02, 2014 at 05:21 AM · animation2d3danimatorchild

How to have an animation loop only twice

Hey guys, Im new to animation, but ive attached a 2d animation to a 3d object in the inspector. so far I've made it so the animation plays once when the child 2d animation is set to active. I would like it to play 2 or more times. How do I set an animation to play only a certain number of times and then disappear after it is done? You do not need to code for me, just point me in the right direction. Thanks,

Images for reference!!

The curvey slash thing is what i want to loop twice (shown below) http://imgur.com/MZgqQDA

here it is attached to the gameobject as a child http://imgur.com/BuR8Anf

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 kacyesp · Sep 02, 2014 at 05:26 AM

I think this is what you're looking for: http://docs.unity3d.com/ScriptReference/Animation.Play.html

Just use the play() function in a script to play the animation however many times you want.

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 Klarax · Sep 02, 2014 at 09:58 AM 0
Share

i didnt know of this function. thank you :)

avatar image camtronius · Sep 03, 2014 at 06:41 AM 0
Share

@kacyesp I tried using animation.play and ran into a bunch of problems. first of all I have this animation on a child object, which is fine as I call the object like this :

 spinAnim = transform.Find("spinslash2d").gameObject;
 
         spinAnim.animation.Play ("Slash");

I set the inspector to debug mode and changed the animations to "1" if you understand what I mean. THe problem im having is that the animations dont appear at all and there is no errors... also, the animations work fine when i use an animation controller... If there is a way to do what I want using an animation controller that would be great because then I could take that route. Do you know how? If not, do you know what my problem would be or some things I could check? Thanks,

avatar image kacyesp · Sep 03, 2014 at 07:09 AM 0
Share

@camtronius Yeah you can use an animator. In the animator window, create a parameter of type int, and let's just call it Counter. Add a transition from the state you want to play twice to some other state. Set that transition's condition to Counter greater than 1. In your actual script, you'll create a Counter variable there too just so you can use something like animator.SetInteger( "Counter", Counter ). Here's the link to the docs for that function: http://docs.unity3d.com/ScriptReference/Animator.SetInteger.html

Then create a function, say IncrementAnimationCounter(), in the script whose sole job is use that SetInt function and increment the Counter.

Finally, add an event to the last keyframe of your animation, where that event will call IncrementAnimationCounter(). If you've never added an event before, see this link for how to do it: http://docs.unity3d.com/$$anonymous$$anual/animeditor-AnimationEvents.html

You can play around with the Counter value in case I'm off by 1, but this is how I would do it. Don't forget to reset the Counter.

avatar image camtronius · Sep 05, 2014 at 02:51 AM 0
Share

@kacyesp took me a long time to finally get it but i got it. Wow. 1 more question though. How do I get it from always having the first frame on my object? In otherwords, I dont want to see any of the animation before it is played. Thanks 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.

Follow this Question

Answers Answers and Comments

23 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 avatar image avatar image

Related Questions

2D Animation does not start 1 Answer

Animator State Freezing 0 Answers

How to optimize 2D Animations created with 2D Animation packege? (v3.1.1) 0 Answers

Animation Lag when Changing Movement Direction 2D (4.3) 2 Answers

Animation stops rotation 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