• 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 · Feb 25, 2016 at 04:17 PM · animationunity5animationstate

Having trouble controlling sprite states the way I want

Alright guys. New to Unity here. I've got a pretty simple game where the main player can only move left and right, avoiding obstacles.

I've got 9 states to achieve this, each 5 frames each. They are:

Straight Loop (just going down the hill, not pressing left or right)

Left Loop (looping animation while you hold left arrow)

Right Loop (looping animation while you hold right arrow)

And then my big issues come with the transition animations between those states. I have:

Straight to Left (transition animation from straight to left, then to Left Loop)

Straight to Right

Left to Straight (transition animation when you let go of left arrow)

Right to Straight

Left to Right (transition animation when you go from left arrow to right arrow)

Right to Left

As of now everything almost works. I can move left and right and the animations play the way I want (so long as I don't try going right while the player is any of the left states, or vice versa. It only works properly if I go left or right from the Straight Loop state). My problems lie in the RightToLeft and LeftToRight states.

Here's my setup as of now alt text

Here is my main code (in Update() on the player controller). It's probably pretty atrocious and I'm sure there's a more efficient way to achieve this.

             if (Input.GetAxis ("Horizontal") < 0) {
                 if (GlobalControl.direction == "right") { //left to right
                     resetAllTriggers ();
                     animator.SetTrigger ("playerLeftToRight");
                 } else {
                     resetAllTriggers ();
                     animator.SetTrigger ("playerStraightToLeft");
                 }
                 GlobalControl.direction = "left";
             }
             else if (Input.GetAxis ("Horizontal") > 0) {
                 if (GlobalControl.direction == "left") { //right to left
                     resetAllTriggers ();
                     animator.SetTrigger ("playerRightToLeft");
                 } else {
                     resetAllTriggers ();
                     animator.SetTrigger ("playerStraightToRight");
                 }
                 GlobalControl.direction = "right";
             }
             else if (Input.GetAxis ("Horizontal") == 0) {
                 if (GlobalControl.direction == "left") {
                     resetAllTriggers ();
                     animator.SetTrigger ("playerLeftToStraight");
                 }
                 if (GlobalControl.direction == "right") {
                     resetAllTriggers ();
                     animator.SetTrigger ("playerRightToStraight");
                 }
     
             }


I'm pretty sure it has to do with my GlobalControl.direction variable getting called at improper/wrong times. I've tried setting this variable in the OnStateExit of some of the states but I just can't quite figure it out. I basically need to bypass everything else when I'm, for example, holding left and then immediately go right (or vice versa).

I really hope this post and my code makes sense, but any help or suggestions would be really great! I'm been trying to wrap my head around this for a day now and just can't quite seem to get it. Maybe I need more than just the direction variable? Or maybe I should be checking to see what animations are playing? I'm not really sure.

Thanks for reading, I really appreciate any help anyone can offer! Thanks so much.

unity-animations.jpg (153.3 kB)
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

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.

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

85 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

Related Questions

Animation which uses trigger is played twice before going back to the previous state. 0 Answers

How do I play an animation state after another animation (like a toggle)? 0 Answers

Mecanim, get state percent complete when a transition occurs? 0 Answers

Trigger to reverse current animation state 1 Answer

Horizontal BlendTree Animations Not Working 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