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 Tricephalus · Jul 16, 2014 at 09:13 PM · animationduplicateframe

How to start the animation of a duplicate object on a different frame

Hi everyone!

Let's see if I can explain myself clearly:

I have a gameObject with an animation of 10 frames attached to it. I need to duplicate the object several times, and I would like to alter the animation on each of the duplicates so the animation starts one frame ahead.

so the original would start at frame 0, the first duplicate at frame 1, the second duplicate at frame 2, and so on.

Is there anyway to do this, without having to create several different animations, or, if possible without having to create a single script for each of the duplicates?

If not, how could I do it anyway? I've been having trouble finding a way to actually control the animations frame by frame.

Thank you in advance

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 Saad_Khawaja · Jul 16, 2014 at 10:56 PM

Unity does not have a method to goto a specific frame per say. But you can move to a specific time interval of an animation using Gameobject.animation["name"].time - Legacy Animation

So in short, you can actually goto a specific frame by finding the time of the animation at that particular frame. e.g. If the animation speed is 24 fps: It means 24 frames are rendered in 1 sec.

The first frame will be at 1/24 1. The second will be at 1/24 2. and so on.

So this snippet will do the trick:

 gameobjectname.animation.Play ("Take 001");
 gameobjectname.animation.Stop ();
 gameobjectname.animation["Take 001"].speed = 0; //to make the animation pause
 gameobjectname.animation["Take 001"].enabled = true;
 gameobjectname.animation["Take 001"].time = (1f/24f)*frameNumber;



Comment
Add comment · Show 8 · 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 Saad_Khawaja · Jul 16, 2014 at 10:59 PM 1
Share

Just to add, you can find the speed of the animation in the animation window. (The samples count if i'm not wrong)

avatar image Tricephalus · Jul 17, 2014 at 06:46 AM 0
Share

Thank you for your answer, that seems like it will do the trick.

I guess it also means I will need to have a separate script for each of the duplicates (which will add up to several dozen)... it won't look particularly good, but if it's the only way I'll do it :)

avatar image Saad_Khawaja · Jul 17, 2014 at 07:51 AM 1
Share

Why do you need to have a separate script for each duplicate?

You can easily create a function and use that to instantiate and set respective frames: for e.g.

 void create$$anonymous$$ultipleObjects()
     {
         for(int i=0;i<10;i++)
         {
             GameObject tmpObj = GameObject.Instantiate(Object1,Vector3.one,Quaternion.identity) as GameObject;
             setFrame(tmpObj,i+1);//Set Frame Number as i+1 so it ranges from 1 to 11 for eg.
         }
     }
 
     void setFrame(GameObject gObj, int frameNumber)
     {
         gObj.animation.Play ("Take 001");
         gObj.animation.Stop ();
         gObj.animation["Take 001"].speed = 0; //to make the animation pause
         gObj.animation["Take 001"].enabled = true;
         gObj.animation["Take 001"].time = (1f/24f)*frameNumber;
             gObj.animation.Play ("Take 001"); //Start animation from specified frame again
     }
 

avatar image Saad_Khawaja · Jul 17, 2014 at 09:39 AM 1
Share

Yes, you're right. And I like your idea. But I don't think you should make 12 scripts when one can do the job:

You can use a public variable frameNumber and then change it for any GameObject that you attach it to.

For e.g.

 public int frameNumber;
 
 void Start()
     {
         gameObject.animation.Play ("Take 001");
         gameObject.animation.Stop ();
         gameObject.animation["Take 001"].speed = 0; //to make the animation pause
         gameObject.animation["Take 001"].enabled = true;
         gameObject.animation["Take 001"].time = (1f/24f)*frameNumber;
         gameObject.animation.Play ("Take 001"); //Start animation from specified frame again
     }
 
avatar image Saad_Khawaja · Jul 17, 2014 at 05:43 PM 1
Share

Okay, with sprite animation its totally different : I would suggest the following: (Do not make animation using animator component/$$anonymous$$ecanim. Remove it as you won't need it)

  1. Download this SpriteAnimator.cs script by Luis Santos.

  2. forum.unity3d.com/attachments/spri$$anonymous$$nimator-cs.104362/

  3. Attach this script to your Coin game object

  4. Fill the inspector values - Attached alt text

screen shot 2014-07-17 at 10.36.42 pm.png (26.9 kB)
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

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

Duplicate Animator Controller problem - models still using the previous controller 1 Answer

Determine in which frame is an Animation currently 2 Answers

[SOLVED] AnimationState.time doesn't work and returns NullReferenceException error 1 Answer

How to change the intervals between frames in the animation window? 1 Answer

2D Game change object's position based on animation. 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