- Home /
Creating 2D art in a 3D world
I have to draw 2D sprites that follow a 3d object for a school project we are doing in Unity. It should look like something like this:
I am somewhat new to the whole 2d on 3d integration. I have the images (about 15) so I need to know how to create a method that can loop these images during run time. So, can someone show me a good starting point on how I should go about this? Thank you in advance.
Answer by x_ivan_x@live.nl · Dec 09, 2013 at 07:16 PM
Create a quad/plane and put a material on that quad/plane with frame 1 attached. Make sure that all the frame pictures are in separate picture files & that the dimensions(x & y) are the same from frame 1 till 5. Assign them in the inspector.
using UnityEngine;
using System.Collections;
public class SwitchBitmap : MonoBehaviour {
public float frameSwitchSpeed = 0.25f;
public Texture[] sprites;
void Start()
{
UpdateSprite(0);
}
void UpdateSprite(int i)
{
renderer.material.mainTexture = sprites[i];
StartCoroutine("LoopSprites", i);
}
IEnumerator LoopSprites(int i)
{
i++;
if(i >= sprites.Length)
i = 0;
yield return new WaitForSeconds(frameSwitchSpeed);
UpdateSprite (i);
}
}
The class runs fine but there is an issue where the sprites are not being drawn on the screen. Is there something I may have missed???
Do you have the quad/plane object pointing towards the camera in game? Also, are there any errors in the console?
Note that this script only loops through the sprites, it doesn't follow an object. here's a link that helps you with that: http://docs.unity3d.com/Documentation/ScriptReference/Vector3.SmoothDamp.html http://docs.unity3d.com/Documentation/ScriptReference/Transform.LookAt.html Basically make it follow the 3d object & make it look at the camera.
-edit sorry, i misunderstood your question. i got confused with the 2d/3d thing. Please explain this concept in more detail. Are you trying to make a 2d game or is that just a top view of something 3d that you're trying to create?
do i write the lookup all in the same sprite animator class?
@x_ivan_x@live.nl: You are right. What I am trying to accomplish is to place a 2d sprite in a 3d world; kind of like GUI. But I require a way to loop an array of 2d sprites (animate them). The class you provided above works! In the inspector I can see the sprite images looping but in the game world nothing shows up. Here is how I am setting everything up: 1 - created a class and pasted your code there. 2- when i go to into unity and try to play it it gives me an error saying that the class (the sprite animation class) is trying to access rendered which I have not placed in the game, so I added 2D sprite renderer and that seemed to have taken care of that error. 3- I play it but nothing shows up.
And that is where I am at right now.
Answer by CoalCzar · Dec 09, 2013 at 09:26 PM
For Unity 4.3, you are on the right track with the Sprite Renderer. And x_ivan_x@live.nl's class will work if you want a scripting method, however, there is now a different (and I think easier) way.
In the Project window, select all your textures for the animation.
In the Inspector window, click on the Texture Type drop down box and select Sprite.
(If you know what settings you want for the textures, go ahead and do that as well)
Press Apply.
Go back to the Project window. With all the textures selected, drag them into an empty space in the Hierarchy.
Save the animation at the prompt.
You can now edit the animation in the Animation window and trigger it using scripts and the Animator window.
I do not understand steps 5-7 because Unity will not let me get that far. When I select what I want to select it does not let me drag the sprites into empty space in the hierarchy.
Hmm, and they all are labeled as a Sprite Texture Type in the inspector? I tested all the steps as listed before I posted my answer.
Yep. I made double sure. It will not let me take the images and put them in the hierarchy
Are you able to drag even one Sprite into the Hierarchy from the Project folder?
Answer by mmangual_83 · Dec 10, 2013 at 04:42 PM
This is the solution to my problem. Many thanks to the people who responded.
public Texture2D[] mSprites;
private Texture2D currentSprite;
private int counter;
public float switchTime = 0.5f;
public Rect r_Sprite;
void Start(){
counter = 0;
StartCoroutine("SwitchSprite");
}
void OnGUI(){
GUI.DrawTexture(r_Sprite,currentSprite);
}
private IEnumerator SwitchSprite(){
currentSprite = mSprites[counter];
if(counter < mSprites.Length){
counter++;
}else{
counter = 0;
}
yield return new WaitForSeconds(switchTime);
StartCoroutine("SwitchSprite");
}