It looks like you're new here. If you want to get involved, click one of these buttons!
So I have a custom script that takes in an array of sprites, and animates them frame by frame. It does this by invoking my custom IncreaseFrame function every 0.1 seconds (and changes the sprite to the next in the array). I have multiple sprites using this, but I want all of their animations to line up because they are conveyor belts, and conveyor belts should be moving at the same pace at the same time. I am not sure how to achieve this. Here's what I've played around with:
Any ideas would be very much appreciated! I have added a gif example of my game.
I did it! Here's my script. It works for any size animation too, because modulo is awesome.
// Switch frame public void SwitchFrame() { // Increase frame frame++; // Get every script that is of sprite animator SpriteAnimator[] spritesToAnimate = GameObject.FindObjectsOfType<SpriteAnimator>(); for(int i = 0; i < spritesToAnimate.Length; i++) { spritesToAnimate[i].frame = (frame % spritesToAnimate[i].frameSPR.Length); spritesToAnimate[i].UpdateFrame(); } // Invoke in a certain amount of seconds, sort of used as speed Invoke("SwitchFrame", frameDuration); }
:D
Answers
Why didn't you use the animator?
I'm not very familiar with the animator, and what I'm using now gives me a lot of control over my frames. It's not too late if I have to switch, it's mostly just because I've only used it a few times and I'm unsure of how to import frame by frame animations efficiently.
I think I should be able to get a list or array of all gameobjects with the animator script and loop through it, activating them individually.
@Roby65 Is right, you should be using the animation editor.
In the Window -> Animation -> Animation tab, you can actually drag in sprites, and create Unity friendly animations that can be easily swapped with the Window -> Animation -> Animator tab.
Some material to get you started:
https://www.youtube.com/watch?v=hkaysu1Z-N8
https://docs.unity3d.com/Manual/AnimationSection.html
Frame by frame animation is totally doable in Unity, using the animation tools will improve your workflow a ton.
You can sync them by playing them in an interval based on Time.time That number will be the same for every object, and will increase consistently. Just write something that plays the animation every time that number increases 5 seconds. (just an example)
Hope this helps!