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 frankyboy450 · Jul 18, 2012 at 10:58 AM · animation2dfast

this animation is too fast. please help

Hi! when I add this script to my 2d character, the animations is just way to fast, and I can't control it.

 var FPS: float;
 var frames : Texture[];
 var running : boolean;
 private var secondsToWait : float;
 private var currentFrame: float;
 
 
 function Start () 
 {
  currentFrame = 0;
  secondsToWait = 1/FPS;
  AnimateRun ();
 }
 
 function AnimateRun () 
 {
  
  if(currentFrame >= frames.Length) 
  {
  currentFrame = 0;
  }
  yield WaitForSeconds (secondsToWait);
  
  renderer.material.mainTexture = frames[currentFrame];
  currentFrame++;
 }
 
 function Update ()
 {
  if(running == true)
  {
  AnimateRun ();
  }
 }


I think that it's a simple script and all and it should work. but it doesn't. Is there some way to make it go slower? thanks

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 Kryptos · Jul 18, 2012 at 11:10 AM 0
Share

The type of currentFrame is surprising. It should be int.

2 Replies

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

Answer by Kryptos · Jul 18, 2012 at 11:16 AM

The problem is that you call a new instance of the coroutine AnimateRun each frame inside the Update function. This is wrong.

You should do something like that (sorry the code is in C#, because I never use UnityScript):

 public float FPS;
 public Texture[] frames;
 public bool isRunning;
 public float secondsToWait;
 public int currentFrame;
 
 IEnumerator Start() 
 {
     currentFrame = 0;
     secondsToWait = 1.0f/FPS;

     while(true)
     {
         if(isRunning)
         {
             yield return StartCoroutine(AnimateRun());
         }
         else
         {
             yield return null;
         }
     }
 }
 
 IEnumerator AnimateRun() 
 {    
     if(currentFrame >= frames.Length) 
     {
         currentFrame = 0;
     }
     yield return new WaitForSeconds (secondsToWait);
 
     renderer.material.mainTexture = frames[currentFrame];
     currentFrame++;
 }



Alternatively, you could check the isRunning variable inside the AnimateRun coroutine. This will ensure that your animation keep in sync.

 IEnumerator Start() 
 {
     currentFrame = 0;
     secondsToWait = 1.0f/FPS;
     StartCoroutine(AnimateRun());
 }
 
 IEnumerator AnimateRun() 
 {
     while(true)
     {
         yield return new WaitForSeconds (secondsToWait);
         if(isRunning)
         {  
             renderer.material.mainTexture = frames[currentFrame%frames.Length];            
             currentFrame++;
         }
     }
 }
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 frankyboy450 · Jul 18, 2012 at 11:29 AM

I followed a tutorial on how to animate in 2d. but it was also in C# this script below works fine but my character script is written in UnityScript.

 using UnityEngine;
 using System.Collections;
 
 public class Animation : MonoBehaviour {
  
  public float FPS;
  private float secondsToWait;
  public bool Loop;
  public Texture[] frames;
  
  private int currentFrame;
  
  // Use this for initialization
  void Start () 
  {
  currentFrame = 0;
  secondsToWait = 1/FPS;
  StartCoroutine(Animate());
  }
  
  IEnumerator Animate()
  {
  bool stop = false;
  
  if(currentFrame >= frames.Length)
  {
  if(Loop == false)
  stop = true;
  else
  currentFrame = 0;
  }
  
  yield return new WaitForSeconds(secondsToWait);
  
  renderer.material.mainTexture = frames[currentFrame];
  currentFrame++;
  
  if(stop == false)
  StartCoroutine(Animate());
  }
 }

I managed to translate it into unityscript but it's not working though. "The type of currentFrame is surprising. It should be int." I searched the unity references site but couldn't find anything about "int" what does it do?

Comment
Add comment · Show 5 · 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 Kryptos · Jul 18, 2012 at 11:52 AM 1
Share

Don't post comment as answer.

int, like float is a type of variable. While float allows digits, int as its name suggests is for integer values.


Your translation was not correct. There was no Update method in the original script. Also the variable currentFrame had the correct type int.

But consider using my version of the script. If find the use of the internal bool inside the coroutine (and the interleaved start of a new coroutine at the end) a bit awkward.

avatar image frankyboy450 · Jul 18, 2012 at 12:15 PM 0
Share

Ok. won't do that from now on.

thanks for explaining ;) I'll try to figure out why this script I have doesn't work

avatar image frankyboy450 · Jul 19, 2012 at 10:25 AM 0
Share

hmm... still not working. unity just shows me alot of errors, Can't see what I'm doing wrong. I wanted to try and call the AnimateRun function in the end of the AnimateRun function so that it would just loop. but it just shows errors to me ;S

avatar image Kryptos · Jul 19, 2012 at 11:25 AM 1
Share

Could you tell us what kind of errors?

avatar image frankyboy450 · Jul 20, 2012 at 01:14 PM 0
Share

ok. I tried using the "InvokeRepeating" code. and now it works :D Thanks for helping me $$anonymous$$ryptos. I'm very grateful

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Animation does not start 1 Answer

Zombie Cafe style game. 1 Answer

Make 2d Animation Scene 0 Answers

Making transform based animation "snap" when mixing with sprite based animation 2 Answers

2D sprite character movement 3 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