Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 MSZika · Jan 19, 2014 at 06:19 AM · animation2dtrigger2d-platformer

Play 2D animations via trigger parameters

I'm starting with Unity and exploring the 2D Platformer example project to learn some things. I'm already porting it to Windows Phone(just to get experience).

I made GUI buttons and Rects to control the hero. All the buttons are working fine, but the move left and move right buttons dont play the "Run" animation. Looking at the scripts I can see that they use triggers to run animations, like anim.SetTrigger("Jump");, so I opened the Animation Controller named character and added a new Trigger to the parameters called "Run", on the scripts I added the following code to the buttons script: anim.SetTrigger("Run");.

The buttons are working fine, but they dont play the run animation, it looks like the hero is floating on the ground.

Here is the buttons code:


 if (GUI.RepeatButton(new Rect(250,Screen.height - 200,200,200), rightIcon, framestyle))
         {
             float h = 1;
             anim.SetFloat("Speed", Mathf.Abs(h));
             if(h * rigidbody2D.velocity.x < maxSpeed){                
                 // ... add a force to the player.
                 rigidbody2D.AddForce(Vector2.right * h * moveForce);
             }
             //Debug.Log ("velocity.x :" + rigidbody2D.velocity.x);
             
             // If the player's horizontal velocity is greater than the maxSpeed...
             if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)
                 // ... set the player's velocity to the maxSpeed in the x axis.
                 rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);
             
             // If the input is moving the player right and the player is facing left...
             if(h > 0 && !facingRight)
                 // ... flip the player.
                 Flip();
             // Otherwise if the input is moving the player left and the player is facing right...
             else if(h < 0 && facingRight)
                 // ... flip the player.
                 Flip();
         }
         
 if (GUI.RepeatButton(new Rect (50,Screen.height - 200,200,200), leftIcon, framestyle))
         {
             float h = -1;
             anim.SetFloat("Speed", Mathf.Abs(h));
             if(h * rigidbody2D.velocity.x < maxSpeed){                
                 // ... add a force to the player.
                 rigidbody2D.AddForce(Vector2.right * h * moveForce);
             }
             //Debug.Log ("velocity.x :" + rigidbody2D.velocity.x);
             
             // If the player's horizontal velocity is greater than the maxSpeed...
             if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)
                 // ... set the player's velocity to the maxSpeed in the x axis.
                 rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);
             
             // If the input is moving the player right and the player is facing left...
             if(h > 0 && !facingRight)
                 // ... flip the player.
                 Flip();
             // Otherwise if the input is moving the player left and the player is facing right...
             else if(h < 0 && facingRight)
                 // ... flip the player.
                 Flip();
         }



It's inside the OnGUI() of the PlayerController and I defined all the variables. I think that I need to attach the "Run" trigger to the "Run" animation, but I don't know how.

Someone can help me?

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

1 Reply

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

Answer by gfoot · Jan 20, 2014 at 05:04 AM

In the Animator window, with the game object selected, you'll see nodes for all the clips the character can play, with arrows between the nodes showing the possible transitions. You probably need to hook your parameter up to some of the transitions.

Just click on a transition arrow, then in the inspector you'll see a "Conditions" box that defines the conditions that cause the transition. The default is based on Exit Time, meaning it transitions when the previous animation has nearly finished. Change that to be based on your new parameter (which should appear in the drop-down list automatically if you added it correctly).

Or, if there are transitions missing, add them by right-clicking on the state to transition from, and using the menu option there.

Running your game with the Animator window visible can help to diagnose state change errors.

Comment
Add comment · Show 2 · 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 MSZika · Jan 20, 2014 at 05:52 AM 0
Share

Thanks man. :) ' With that I figured out that the "Run" animation is triggered when the float parameter "Speed" is higher than 0, so I fixed the buttons code.


Another noob question: how do I close the game on Windows Phone through the scripts? (like an exit button)

avatar image gfoot · Jan 20, 2014 at 10:38 AM 0
Share

Cool. I don't know Windows Phone but generally you call Application.Quit() to quit your app.

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

19 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

Related Questions

How do i make a text box appear on screen after a chest has been opened? 3 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How do I make animation transitions a one-way street? 1 Answer

Sliding door animation question. 3 Answers

How to start a boss fight??? 1 Answer


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