Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 jterren · Apr 24, 2018 at 08:01 PM · animationscripting beginnerspritesspritesheet

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.

Comment
Add comment · Show 7
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 TreyH · Apr 24, 2018 at 08:13 PM 1
Share

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?

avatar image jterren · Apr 24, 2018 at 08:34 PM 0
Share

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);
 }

}

alt text

blend.png (201.6 kB)
avatar image jterren · Apr 24, 2018 at 09:42 PM 0
Share

this is an example of one of our sprite sheets.

alt text

cc-00000.png (11.3 kB)
avatar image TreyH jterren · Apr 24, 2018 at 10:43 PM 1
Share

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)

avatar image jterren · Apr 24, 2018 at 10:46 PM 0
Share

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.

avatar image TreyH jterren · Apr 24, 2018 at 10:50 PM 1
Share

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.

avatar image jterren TreyH · Apr 25, 2018 at 12:39 AM 0
Share

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);
     }
 }

}

0 Replies

· Add your reply
  • Sort: 

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

223 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 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 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 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 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 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 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 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 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 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

add animation frames to existing spritesheet with animations? 0 Answers

how to display a single frame of a sprite animation 1 Answer

Animation with different size sprites 0 Answers

Create animation from script 0 Answers

What are the best practices for reusing an animation controller? 0 Answers


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