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 Landcross · Apr 09, 2015 at 04:29 AM · animation2danimatorrandom

How to randomly start & stop an animation?

I'm fairly new to Unity (and C#). At the moment I'm trying to make a simple 2D platformer.

I have some items in my game that the player can collect for points. I have a very simple animation for those items (it's a diamond that shines). But, I don't want to have all the diamonds keep shining and shining. I want them to shine and then wait a random amount of time before they shine again.

I tried something with two animations (an idle state and a shining state) and I also googled a bit here and there and read about some other things. I just can't get it to work.

It's not the first time I have problems with the animator in Unity. In my experience so far I find it quite annoying. I don't know why...

Anyway, I hope someone has a simple solution for me?

Thanks!

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

Answer by DoTA_KAMIKADzE · Apr 10, 2015 at 02:51 PM

I guess that is what you want:

1)Add boolean parameter "Start" (you can name it as you want I'll just use that name in example) to your animator transitions. If true => set shine animation, if false => set idle animation.

2)Add this code to your diamond prefab:

 public class Diamond : MonoBehaviour
 {
     static bool diamondsCanShine = true;
     private Animator _anim;
     [SerializeField]
     private string startAnimation = "Start";
     internal bool isAnimated
     {
         get { return _anim.GetBool(startAnimation); }
         set { _anim.SetBool(startAnimation, value); }
     }
     private void Awake()
     {
         _anim = GetComponent<Animator>();
     }
     private void Start()
     {
         StartCoroutine(Example());
     }
     IEnumerator Example()
     {
         while (diamondsCanShine)//replace with something like "GameIsPlayedBoolean" or "LevelIsRunning" or "GameIsNotPaused" etc.
         {
             isAnimated = !isAnimated; //this will start and stop animation if animation never stops
             //though if your animation is not loooping and stops then you can always randomize boolean istead or check if (isAnimated) or (!isAnimated) and do what you expect accordingly
             yield return new WaitForSeconds(Random.Range(0f, 10f));//this will randomly wait next time the process changes, set min and max wait time
         }
     }
 }
Comment
Add comment · Show 6 · 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 Landcross · Apr 10, 2015 at 03:56 PM 0
Share

Thanks a lot!

$$anonymous$$y own code was quite close to yours, but it looks like you fixed some things I wasn't able to get working.

However, I can't test it, because, well, I don't completely understand how to accomplish step 1. I added a boolean parameter called "Start" into the animator, but how do I make a transition using that boolean?

avatar image DoTA_KAMIKADzE · Apr 10, 2015 at 04:36 PM 2
Share

Watch this tutorial.

avatar image Landcross · Apr 10, 2015 at 06:08 PM 0
Share

Ah, I see. I was able to make a transition with the parameter to the shine state. But, I can't edit (add the parameter) on the transition to the default state (the orange block). I can only add a 2nd transition to the default state.

I hope that's understandable...

Anyway, I appreciate your help, but I also understand if... well, if you start to find my questions annoying ;)

avatar image DoTA_KAMIKADzE · Apr 10, 2015 at 06:15 PM 1
Share

You can edit transitions - click with left mouse(L$$anonymous$$B) on the transition "arrow" in order to edit transition.

P.S. Just in advance - to delete transition select it and press Delete button.

avatar image DoTA_KAMIKADzE · Apr 10, 2015 at 06:54 PM 1
Share

Transition from entry just defines the first state node, you can change it by clicking with right mouse (R$$anonymous$$B) on any other transition and setting it as "Default State".

As for the last - check the exit time, transition duration and offset. That is all configurable under transition settings.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

2D Animation does not start 1 Answer

Damn, HOW CAN I KNOW, IF MY 2d ANIMATION SPRITE IS FINISHED??!! 0 Answers

Best way to do animate soldier? 1 Answer

Animation transform lock 0 Answers

How can i play a animation? 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