- Home /
how to make a basic 2d animation
so i have 33 different textures (of the same pciture, each one slightly different from the last). I want to animate them in a way that will replace the last picture with the new one, and i want it to go through all 33 of them in 2 seconds. the images im using are 2d, but they will be attached to a quad (this isnt a 2d game). How do i do this in unity? (ive never used the animation tools before). Do they need to be imported as sprites and used in the animator or could i just as easily do this through code?
Edit: i set all of the textures to materials and im having trouble actually cycling through them all for exactly two seconds. Every time i try to run unity now, the game freezes. heres my code
using UnityEngine;
using System.Collections;
public class CursorAnimation : MonoBehaviour {
float elapsedTime;
public float timer;
Material c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17,
c18, c19, c20, c21, c22, c23, c24, c25, c26, c27, c28, c29, c30, c31, c32;
int spot;
Material[] c_array;
void Start()
{
c0 = Resources.Load ("mat_reticle", typeof(Material)) as Material;
c1 = Resources.Load ("mat_c1", typeof(Material)) as Material;
c2 = Resources.Load ("mat_c2", typeof(Material)) as Material;
c3 = Resources.Load ("mat_c3", typeof(Material)) as Material;
c4 = Resources.Load ("mat_c4", typeof(Material)) as Material;
c5 = Resources.Load ("mat_c5", typeof(Material)) as Material;
c6 = Resources.Load ("mat_c6", typeof(Material)) as Material;
c7 = Resources.Load ("mat_c7", typeof(Material)) as Material;
c8 = Resources.Load ("mat_c8", typeof(Material)) as Material;
c9 = Resources.Load ("mat_9", typeof(Material)) as Material;
c10 = Resources.Load ("mat_c10", typeof(Material)) as Material;
c11 = Resources.Load ("mat_c11", typeof(Material)) as Material;
c12 = Resources.Load ("mat_c12", typeof(Material)) as Material;
c13 = Resources.Load ("mat_c13", typeof(Material)) as Material;
c14 = Resources.Load ("mat_c14", typeof(Material)) as Material;
c15 = Resources.Load ("mat_c15", typeof(Material)) as Material;
c16 = Resources.Load ("mat_c16", typeof(Material)) as Material;
c17 = Resources.Load ("mat_c17", typeof(Material)) as Material;
c18 = Resources.Load ("mat_c18", typeof(Material)) as Material;
c19 = Resources.Load ("mat_c19", typeof(Material)) as Material;
c20 = Resources.Load ("mat_c20", typeof(Material)) as Material;
c21 = Resources.Load ("mat_c21", typeof(Material)) as Material;
c22 = Resources.Load ("mat_c22", typeof(Material)) as Material;
c23 = Resources.Load ("mat_c23", typeof(Material)) as Material;
c24 = Resources.Load ("mat_c24", typeof(Material)) as Material;
c25 = Resources.Load ("mat_c25", typeof(Material)) as Material;
c26 = Resources.Load ("mat_c26", typeof(Material)) as Material;
c27 = Resources.Load ("mat_c27", typeof(Material)) as Material;
c28 = Resources.Load ("mat_c28", typeof(Material)) as Material;
c29 = Resources.Load ("mat_c29", typeof(Material)) as Material;
c30 = Resources.Load ("mat_c30", typeof(Material)) as Material;
c31 = Resources.Load ("mat_c31", typeof(Material)) as Material;
c32 = Resources.Load ("mat_c32", typeof(Material)) as Material;
c_array = new Material[]{c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17, c18, c19, c20, c21, c22, c23, c24, c25, c26, c27, c28, c29, c30, c31, c32};
timer = 2f;
elapsedTime = 0;
spot = 0;
}
public void animateCursor()
{
timer = 2.0f;
elapsedTime = 0;
while (timer > 0) {
if (2.0 / 33 * (spot + 1) > elapsedTime)
spot++;
if (spot > c_array.Length - 1) spot = 32;
Debug.Log("SPOT: " + spot);
Debug.Log("Timer: " + timer);
GetComponent<Renderer> ().material = c_array [spot];
}
if (spot >= c_array.Length)
spot = 0;
timer = 2f;
elapsedTime = 0;
GetComponent<Renderer> ().material = Resources.Load ("mat_reticle", typeof(Material)) as Material;
}
void Update()
{
timer -= Time.deltaTime;
elapsedTime += Time.deltaTime;
}
}
Answer by tyxman · Jun 29, 2015 at 09:15 PM
Why don't you create a animation using Sprites and attach that to the quad instead. When creating the animation add each new picture as a new frame.
i couldnt figure out how to do that before. I got it now though
If you need help I've just done that exact thing the other day so feel free to ask.
actually i am having trouble with this. i changed all the textures for the material to sprites and im trying to add them to the animation, but i cant figure out how to actually do that. Pretty much everything in the animation window is greyed out and dragging them into the inspector for the animation doesnt seem to do anything either. So.. how do i actually add sprites to the animation?
If it's grayed out that usually means you haven't created the .anim file so either in the top left of the animation window or the project window create a new animation file called whatever open that in the animation window. Then add the sprites. I got this idea from this tutorial https://unity3d.com/learn/tutorials/modules/beginner/2d/2d-controllers the animation instructions starts at around 20:00 $$anonymous$$ so try that
the link isnt working... Could the fact that Im making a 3d game be the reason it doesnt work?
Your answer
Follow this Question
Related Questions
Select Animations Freezing on First Frame 1 Answer
2D Animation does not start 1 Answer
Animation not playing Unity 5 (2D) 0 Answers
SpriteManager 2 1 Answer
Best way to do animate soldier? 1 Answer