• 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 /
  • Help Room /
avatar image
0
Question by subver · Mar 17, 2016 at 07:08 PM · animationunity5spritesstate machine

Best method for controlling 2D sprite with transition/in-between animations?

I've got a pretty basic setup:

Player can only move left or right. I have looping animations for Idle, Left and Right.

I also have animations that play between all of them: "Idle to Left," "Left to Idle," "Idle to Right," "Right to Idle," "Left to Right" and "Right to Left"

I did originally set this all up using triggers, the state machine looks like an insane web, and for the most part it works, but it's buggy. The player will get stuck in certain animations sometimes, or won't continue through the transition animation, etc.

I'm sure my code is the culprit, but I'm looking for a way to redo the whole thing properly.

Are there any tutorials out there for this? All I can find are controlling basic sprites: Idle to Run, Idle to Jump, etc... nothing with transition animations.

I'm not sure if I should set this up using triggers, having a "Speed" float, bools, etc, or a combination of them.

I just feel like I am making this way more complicated than it has to be.

Here is the main part of my code that controls the animation:

 if (moveHorizontal < 0) { 
     if (GlobalControl.direction == "right") {
         resetAllTriggers ();        
         animator.SetTrigger ("playerRightToLeft");
     } else {
         resetAllTriggers ();
         animator.SetTrigger ("playerStraightToLeft");
         GlobalControl.direction = "left";
     }
 
 }
 
 if (moveHorizontal > 0) {
 
     if (GlobalControl.direction == "left") {
         resetAllTriggers ();
         animator.SetTrigger ("playerLeftToRight");
     } else {
         resetAllTriggers ();
         animator.SetTrigger ("playerStraightToRight");
         GlobalControl.direction = "right";    
     }
 }
 
 if (moveHorizontal == 0 
     && (animator.GetCurrentAnimatorStateInfo (0).IsName ("Player-LeftToRight") == false) 
     && (animator.GetCurrentAnimatorStateInfo (0).IsName ("Player-RightToLeft") == false)) { 
 
     if (GlobalControl.direction == "left") {
         resetAllTriggers ();
         animator.SetTrigger ("playerLeftToStraight");    
 
     } else if (GlobalControl.direction == "right") {
         resetAllTriggers ();
         animator.SetTrigger ("playerRightToStraight");
     }
 }


The GlobalControl.direction is also being set in the "Left to Right," "Right to Left," and "Right/Left to Straight" onStateExit() callback.

It all just seems so convoluted and I feel there must be a better way!

Any suggestions on how to go about this would be great! Thanks so much.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by J0hn4n · Jul 18, 2017 at 01:38 AM

Hello man i suggest making a sort type of fsm to movement/animation logic, actually having all this in code its a lot more usefull. Use blend trees and just call animator.play("AnimName");

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
0

Answer by Zoogyburger · Mar 17, 2016 at 08:39 PM

This tutorial should help you get the basics of what you need to do:

https://www.youtube.com/watch?v=TU6wflRqT5Q

Here's the script

      Animator anim;
      
      // Use this for initialization
      void Start () {
          anim = GetComponent<Animator>();
          
      }
      
      // Update is called once per frame
      void Update () {
  
      
          float input_x = Input.GetAxisRaw("Horizontal");
          float input_y = Input.GetAxisRaw("Vertical");
          
          bool isWalking = (Mathf.Abs(input_x) + Mathf.Abs(input_y)) > 0;
          
          anim.SetBool("isWalking", isWalking);
          if(isWalking)
          {
              anim.SetFloat("x", input_x);
              anim.SetFloat("y", input_y);
              
              transform.position += new Vector3(input_x, input_y, 0).normalized * Time.deltaTime;
              
              
          }
      }
  }
Comment
Add comment · Show 7 · 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 subver · Mar 17, 2016 at 08:52 PM 0
Share

I have part of the basics down, but I defnitely need to learn more. I was thinking of utilizing the blend tree but still am not quite sure of how to go about it, I have a lot of experimenting to do. This is the kind of tutorial I was talking about, though, there are no transition animations, just idle to up, idle to left, idle to right, etc if that makes sense.

I just watched this tutorial, though: https://www.youtube.com/watch?v=Xnyb2f6Qqzg and it gave me some ideas for using the blend tree, I'm just gonna have to watch it again while working on my animation.

$$anonymous$$y biggest hurtle is getting the transition animations to all play properly and not get stuck.. especially when you move left/right rapidly a few times (or during other transition animations) things get mucked up and the sprite will be in an idle state with the left-loop animation playing.

I think I also need to utilize the "Any State" to do a lot of the transitions.. I don't think I even realized that was a thing when I first coded this sprite.

I need to really think it through and maybe write it all down logically on paper first and just rethink how I want to go about it, I just wasn't sure if anyone had suggestions on the whole "transition/in-between animation" side of things.. I can't find much online about 2D sprites working this way.

avatar image Zoogyburger · Mar 17, 2016 at 10:20 PM 0
Share

I'm Sorry, I don't understand what movement you are trying to do:( $$anonymous$$aybe you could post a link to a video of some sort that tells me what you are looking for

avatar image subver Zoogyburger · Mar 17, 2016 at 10:46 PM 1
Share

Let me try to explain it better, sorry about that.

Ok, so my player can go left and right, or straight/idle. I have 3 main looping animations, "Left," "Right" and "Straight" (Idle).

When going from "Straight" to "Left" or "Right" it doesn't just immediately pop into the "Left" animation, it plays through "Straight to Left" then stays on "Left" - "Straight To Left" is the transition animation I'm talking about.

The ones that I think screw my animations up (or that makes my code way more convoluted) is when the player immediately goes Left to Right, bypassing any animations that go back to straight. So there is a quicker transition animation "Left to Right" or "Right to Left."

$$anonymous$$y code works fine if I let all the animations play, the real trouble comes when I am changing direction in the middle of transitions. As of now I've got a whole mess of transitions for each animation state in case the player changes direction during one of those transition animations.

I just think I am going about this all wrong.. after seeing that I can use "Any State" and the Blend Tree (which I still need to learn how to use) I think I can figure out a better way.. I just feel as though this would be a common way to set up character animations but I really can't seem to find much out there about this. They are all just immediate changes in direction, ins$$anonymous$$d of having an in-between/transition animation from one main state to another.

Sorry for the wall of text haha, I hope I explained it properly!

avatar image Zoogyburger · Mar 18, 2016 at 06:01 AM 0
Share

Okay i get it. You could do this in script but it would be rather messy... You could have your Straight animation state in between of left and right so that they would have to pass through the Straight animation to get to the other one. I would put a screenshot of what I'm saying but for some reason I'm having a hard time uploading screenshots on this site:(

avatar image subver Zoogyburger · Mar 18, 2016 at 02:23 PM 0
Share

I definitely get what you're saying. I was going to do that initially but I wanted a quicker animation from left to right, but I might be able to accomplish that with the blend tree? When I get a $$anonymous$$ute I really want to learn how to use that better for sure.

avatar image Zoogyburger · Mar 18, 2016 at 05:44 PM 0
Share

I think this can be done with blend trees. There is a ton of blend tree tutorials out there:

http://docs.unity3d.com/$$anonymous$$anual/BlendTree-1DBlending.html https://unity3d.com/learn/tutorials/modules/beginner/animation/blend-trees http://docs.unity3d.com/$$anonymous$$anual/class-State.html https://www.youtube.com/watch?v=ihmPDjiF-zg https://www.youtube.com/watch?v=DJQn_E$$anonymous$$EphE

avatar image subver Zoogyburger · Mar 18, 2016 at 05:55 PM 0
Share

Awesome, thanks for the links! Definitely gonna try and revamp this controller over the weekend. Can't wait to start using blend trees!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

93 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

Related Questions

Texture count in Profiler increasing everytime animation is played 0 Answers

How to animated Sprite Shape with Sprite Sheet? 0 Answers

Stop animation at any frame? or have two idle states? 0 Answers

Animation will only run once. 0 Answers

Is it bad practice to perform charcater control/movement from inside state machine behaviours? 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