Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
11 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 Pearbook · Jul 21, 2015 at 01:36 PM · animationspriteanimatorspritesheet

Quickly switch sprite animation

I have a character for a 2D platformer with many different animations, such as idle/running/jumping animations for left and right. These are animations made from a spritesheet (nothing has been done inside of Unity). Currently I'm akwardly switching animation through the Animator window and turning on and off parameters. The problem is that not only the Animator window is very messy, but my script as well. So I'm looking for a better way to switch between animations while using sprite sheet animations. Down below a code preview of how I currently switch between animations:

 // Check and set animations for action when facing right. 
         if(player.Horizontal > 0)
         {
             if(player.isGrounded)
             {
                 // Running.
                 playerAnimator.SetBool("Running_R", true);
                 playerAnimator.SetBool("Idle_R", false);
                 playerAnimator.SetBool("Jump_R", false);
             }
             else
             {
                 // Jumping while running.
                 playerAnimator.SetBool("Jump_R", true);
                 playerAnimator.SetBool("Running_R", false);
                 playerAnimator.SetBool("Idle_R", false);
             }
         }
 
         if(player.Horizontal == 0)
         {
             if(player.isGrounded)
             {
                 // Idle.
                 playerAnimator.SetBool("Idle_R", true);
                 playerAnimator.SetBool("Running_R", false);
                 playerAnimator.SetBool("Jump_R", false);
             }
             else
             {    
                 // Jumping while idle.
                 playerAnimator.SetBool("Jump_R", true);
                 playerAnimator.SetBool("Idle_R", false);
             }
         }

So my question is: How do I switch spritesheet animations more easily and more efficient ?

Comment
Add comment · Show 1
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 game-dev333 · Jul 21, 2015 at 01:59 PM 0
Share

your player will have three animation and transition

Use playerAnimator.SetInteger("AnimationNumber",0) - to play default or idle playerAnimator.SetInteger("AnimationNumber",1) - Running playerAnimator.SetInteger("AnimationNumber",2) - Jump

give all transitions so you can switch between any of them.

hope this will make your work a bit easy

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Pearbook · Nov 27, 2015 at 10:25 AM

I actually forgot about this question I asked. I did find a solution a while back, but I'll share it anyway:

 using UnityEngine;
 using System.Collections;
 
 public class Animation : MonoBehaviour 
 {
     public enum animations
     {
         Idle,
         Walk,
         Jump,
         Fall
     }
 
     public animations currentAnimation;        // Animation that is currently playing.
 
     public Animator Animator;                // The animator you want to use.
 
     void Update () 
     {
         if(/* Idle */)
             TransitionTo(animations.Idle, "NameOfIdleAnimation");
 
         if(/* Moving */)
             TransitionTo(animations.Idle, "NameOfWalkingAnimation");
     }
 
     // Set the animation enum, Name of the animation you want to play.
     void TransitionTo(animation anim, string name)
     {
         // Check if the animation is already playing or not/
         if (currentAnimation != anim)
         {
             Animator.Play(name);
             currentAnimation = anim;
         }
     }
 }

The nice thing about it is that because I use Pixel art graphics I don't need smooth transitions and this solution doesn't require a transition in the Animator. Just an Animator with the animations in it (with the right names of course).

Comment
Add comment · Share
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
1

Answer by alexi123454 · Jul 21, 2015 at 02:02 PM

Rather than using Bools for everything in your animator, you could use Triggers instead. They another parameter type, but they're more suited for rapid changes in state.

http://docs.unity3d.com/ScriptReference/Animator.SetTrigger.html

Instead of changing a number of bool for each thing, your code would look something like this:

  // Check and set animations for action when facing right. 
          if(player.Horizontal > 0)
          {
              if(player.isGrounded)
              {
                  // Running.
                  playerAnimator.SetTrigger("Running_R");
              }
              else
              {
                  // Jumping while running.
                  playerAnimator.SetTrigger("Jump_R");
              }
          }
  
          if(player.Horizontal == 0)
          {
              if(player.isGrounded)
              {
                  // Idle.
                  playerAnimator.SetTrigger("Idle_R");
              }
              else
              {    
                  // Jumping while idle.
                  playerAnimator.SetTrigger("Jump_R");
              }
          }

You would have to change a few little things around in your transitions, but it should be cleaner in the long run.

Comment
Add comment · Show 6 · Share
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 Pearbook · Jul 21, 2015 at 06:06 PM 0
Share

This is code wise a lot cleaner, thanks. I was hoping for a way to clean up my transitions in the Animator view, which currently looks this and hasn't even got all the animations in yet: alt text

avatar image alexi123454 · Jul 21, 2015 at 07:29 PM 0
Share

Ins$$anonymous$$d of having a transition from each thing, you could replace a lot of them by making them transition from the "Any State" bubble. If you call a transition between the Any State and a state, it will do it automatically regardless of what state you're currently in. That should make the animator view a little cleaner :P

avatar image Pearbook · Jul 22, 2015 at 10:33 AM 0
Share

Thanks, the "Any State" is something I was looking for. But I haven't figured out how it works, when I make a transition it keeps repeating that transitions. So my animation only plays 1 or 2 frames and then repeats itself.

avatar image alexi123454 · Jul 22, 2015 at 12:19 PM 0
Share

Are you still using the Boolean parameters ins$$anonymous$$d of the triggers? If you still have boolean parameters set up, it's going to see that the boolean is still set and keep doing the transition.

Say you have "Right" playing. The animator things "oh hey, right should be playing, let's transition to that from any state". Then, once the transition finishes and the program is playing Right, it thinks again "Hey, the transition is finished! But wait a second, right should still be playing. Let's transition to it from any state again". Since Right is a state, and it can transfer to Right from any state, it can transfer to itself, which would cause the the 1-2 frame repeating animations you're getting.

avatar image Pearbook · Jul 22, 2015 at 12:27 PM 0
Share

Thanks for your quick reply, but I already got rid of the booleans but for some reason the triggers have the same effect and some times trigger won't turn of after being used. This is how it looks right now: alt text

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

add animation frames to existing spritesheet with animations? 0 Answers

How do I change animation states... or is there a better way? 1 Answer

Change entire spriteSheet from animation tree 2 Answers

How to increase sprite animation speed 2 Answers

Replace Sprite Sheet that's Already Animted? 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