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 Arvank · Jan 21, 2013 at 01:37 PM · animationjavascriptarrayanimationstate

Using animationState for animation controls

Hey everyone,

I was hoping to get some help with a problem that I've been stuck on for the past week and a half. I am pretty sure that this might end up being a really easy fix, but I've had no luck so far.

I'm working on a project that requires me to create a sort of a "Media Player" setup in Unity3D, specialized in showcasing animation. I've been able to use the AnimationState to create a simple increase/decrease play speed which works at the moment, but I'm pretty much stuck on every single other function.

Now, I have a pretty good idea on what the problem is. So far I've been using AnimationState in the same way that it's described in the examples on the Unity website, which looks like this:

 for (var state : AnimationState in animation)
 {state.speed = 0.5;}


The piece of code that I've used for my function looks like this

The function itself:

 function increaseAnimSpeed(anim:Animation)
 {    
     for(var state: AnimationState in anim)
     {
         state.speed = state.speed * speedChanger;
         Debug.Log("changing to " + state.speed);
     }
 }


Calling the function when a button is pressed:

 if(GUI.Button(Rect(width * 0.45, height * 0.835, 30, 30), "P"))
 {
     PausePlayAnimation(this.gameObject.animation);
     Debug.Log("Play!");
 }

I used this example in my increase/decrease function, but every time that I call the function, it is executed several times. To be precise, I think it executes as many times as there are animations on the gameObject to which the script is attached and once on top of that. Which I imagine is pretty logical, because of the for-loop. For the speed controls this doesn't seem to be that big of a deal. It changes the speed from 1 to 2. but executes the loop 5 times if there are 4 animations on the gameobject. It does the job, no problem, it just runs a bunch of extra unnesscesary loops.

It doesn't work at all for the other functions though, when I try to create the pause/play function, for example, switches between pause and play several times each time I click the button. The code for the function is pasted below:

 function PausePlayAnimation(anim: Animation)
 {
     var isPlaying: boolean = true;
     var tmpSpeed: float;
     
     for(var state: AnimationState in animation)
     {
 
     // Pause the animation
         if(isPlaying == true)
         {
             tmpSpeed = state.speed;
             state.speed = 0;
             isPlaying = false;
             Debug.Log("Pausing animation, state.speed is: " + state.speed + ". tmpSpeed is " + tmpSpeed);
             yield new WaitForSeconds(buttonDelay);
         }
     
     //Play the animation
         else
         {
             state.speed = tmpSpeed;
             isPlaying = true;
             Debug.Log("Resuming animation, state.speed is: " + state.speed + ". tmpSpeed is " + tmpSpeed);
             yield new WaitForSeconds(buttonDelay);
         }
     }
 }

If I call this function, the outpit prints the Debug 5 times and I just have NO idea on how to fix this. I've been looking for a way to use AnimationState without the for-loop, of course. But unfortunatly, I'm not that great of a programmer and I always seem to get null reference errors when I try to just declare it as a variable.

Sorry for the incredibly long slab of text, I'm hoping you guys can help me out with this.

TL;DR Any way to use animationstate without a for-loop, or any alternative way to create a play/pause button?

Cheers!

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
0

Answer by waterlight_ · Apr 25, 2017 at 05:06 AM

your Debug.Log is executed 5 times because of the for loop..

try this:

  function PausePlayAnimation(anim: Animation)
  {
      var isPlaying: boolean = true;
      var tmpSpeed: float;
      var debug: bool = true;
      
      for(var state: AnimationState in animation)
      {
  
      // Pause the animation
          if(isPlaying == true)
          {
              tmpSpeed = state.speed;
              state.speed = 0;
              isPlaying = false;
              if (debug) {
                   Debug.Log("Pausing animation, state.speed is: " + state.speed + ". tmpSpeed is " + tmpSpeed);
                   debug = false;
              }
              yield new WaitForSeconds(buttonDelay);
          }
      
      //Play the animation
          else
          {
              state.speed = tmpSpeed;
              isPlaying = true;
              if (debug) {
                   Debug.Log("Resuming animation, state.speed is: " + state.speed + ". tmpSpeed is " + tmpSpeed);
                   debug = false;
              }
              yield new WaitForSeconds(buttonDelay);
          }
      }
  }

i did not test it but i hope it will help

Comment
Add comment · 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
0

Answer by BloodMarked · Apr 24, 2017 at 07:51 AM

the way i handle speed in the animations is creating a speed parameter in the animator controllerand on the animation state's properties enable the multiplier under speed and set it to the animator controller's speed value. alt text then it is pretty easy to set this value with animation.setfloat from the script.

to pause, i would try setting the speed to 0


speed.jpg (79.7 kB)
Comment
Add comment · 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

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

Making an Array of Animations Play at Random With No Repeats 3 Answers

Random Array of sounds for Random Game Object 1 Answer

passing a javascript array into a function sorts it? 1 Answer

How would you make a shapeshifting mechanic in a game? 1 Answer

Playing sprint animation with vertical axis? 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