- Home /
Trying to create a C script to take 36 sprites from a sprite sheet and create 4 directional animation clips?
So my group and I are creating a game for our Software Engineering class, one of the members made a character creation system that has 434 possible outcomes. The way we have it set up now is based on IDs for each sprite sheet out of the 434, and each time you press an intractable button it changes a value in the ID, and loads up a sprite from our designated preview sprite sheet, which is separate from what I am working with. I plan on taking the final ID, searching a library of the sprite sheets with said ID, and returning then sending that sprite sheet to the next scene. That part I have not done yet but I have a good understanding on how to do it. The problem I am currently having is finding a way to create the needed animations and assign them to my blend tree on start of the scene. I could create all the animations and do a similar library search approach, but the deadline is closing in so I am looking for a better approach. not to mention the actual storage size of the game is getting really big so creating 1600+ animations is kind of out of the question. Any help at all is appreciated.
P.S. Each sprite sheet has 36 sprites, 4 sets of 9 sprites walking in different directions.
I don't think you even need the overhead of making animation clips at all. I'm assu$$anonymous$$g each sheet follows the same pattern?
If that's the case, then you already know the sprite index for each orientation / variation of that orientation. It won't take you too long to just write a simple state machine manually.
Since this is a school project, folks here might be hesitant to give you code explicitly, so what have you tried so far?
I do not want the code explicitly as I like creating my own work. What I would like is the kind of advice you just gave. Yes all my sprite sheets follow the same pattern, so what would a simple state machine look like? As of right now I manually created 4 movements from on sprite sheet and the blend tree attached plays the animation on input.
$$anonymous$$y player controller script passes values to my animator and thus my blend tree: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : $$anonymous$$onoBehaviour {
public float moveSpeed;
private Animator anim;
private bool is$$anonymous$$ove;
private Vector2 last$$anonymous$$ove;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
is$$anonymous$$ove = false;
if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
{
transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
is$$anonymous$$ove = true;
last$$anonymous$$ove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
}
if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
{
transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
is$$anonymous$$ove = true;
last$$anonymous$$ove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
}
anim.SetFloat("$$anonymous$$oveX", Input.GetAxisRaw("Horizontal"));
anim.SetFloat("$$anonymous$$oveY", Input.GetAxisRaw("Vertical"));
anim.SetBool("Is$$anonymous$$ove", is$$anonymous$$ove);
anim.SetFloat("last$$anonymous$$oveX", last$$anonymous$$ove.x);
anim.SetFloat("last$$anonymous$$oveY", last$$anonymous$$ove.y);
}
}
So, you might start by getting that texture's sprites as an array inside of your little state machine script, as well as a reference to your object's SpriteRenderer. Then, you will need to:
Decide which direction should be renderered (0-8, 9-15, etc)
Decide where the sprites corresponding to that direction are
Decide how you want to show and keep track of the variation within that direction (you have 9 variants for each, which shows first, how often do you change it, etc)
So do i even need the blend tree if I am using that script with what you suggested? Also thank you so much for the help, this is due tomorrow so we are really trying to pull all our parts together.
I don't know the restrictions on your assignment, but I wrote something to use rpg maker character sheets for the exact reason you cited in the OP (same process, too much overhead to make animator for each).
If someone on your $$anonymous$$m is really comfortable with them and thinks they can do it, then maybe they have a better plan.
I found a script online that I am trying out to get an animation to play on the screen, it loads up the sprites on run, but it does not play through them.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SpriteAnimator : $$anonymous$$onoBehaviour {
public SpriteRenderer spr;
public Sprite[] Sheet;
public Sprite[] Forward; //This are there until I can actually get an animation to play on screen, after each will be filled with corresponding sprites
public Sprite[] Backward;
public Sprite[] Rightward;
public Sprite[] Leftward;
public string location = "Default";
public bool isLooping;
public bool playOnStart;
public float secToWait;
public float fps;
private int currentFrame;
private bool stopped = false;
//public GameObject blend = GameObject.FindGameObjectWithTag("Player").GetComponent<Animator>().
void Start()
{
Sheet = Resources.LoadAll<Sprite>(location);
}
public void Awake()
{
spr = this.GetComponent<SpriteRenderer>();
currentFrame = 0;
if (fps > 0)
secToWait = 1 / fps;
else
secToWait = 0f;
if(playOnStart)
{
Play(true);
}
}
public void Play(bool reset = false)
{
if(reset)
{
currentFrame = 0;
}
stopped = false;
spr.enabled = true;
if(Sheet.Length > 1)
{
Animate(Sheet);
}
else if (Sheet.Length > 0)
{
spr.sprite = Sheet[0];
}
}
public virtual void Animate(Sprite[] movement)
{
CancelInvoke("Animate");
if (currentFrame >= movement.Length)
{
if(!isLooping)
{
stopped = true;
}
else
{
currentFrame = 0;
}
}
spr.sprite = movement[currentFrame];
if(!stopped)
{
currentFrame++;
}
if(!stopped && secToWait > 0)
{
Invoke("Animate", secToWait);
}
}
}