Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Sugar Mommy · Jul 09, 2015 at 02:39 PM · animation2danimatortransition

[C#] [2D] Animation mostly not working

Hello

I've been working on a small 2D platformer game for my little brother. The main character (a monkey) is supposed to be able to use a sword to damage enemies. I've come across a problem trying to animate this sword. The animation is supposed to be pretty simple:

  1. First frame: sword is disabled

  2. Second frame: sword becomes enabled

  3. Middle frame: sword's X value has changed so it looks like it's thrusting forward

  4. Second to last frame: sword enabled

  5. Last frame: sword disabled

Below, I will include a few pictures of the animation, animator and the code I used to call the animation.

The problem is that sometimes the animation does display correctly, but most of the time it doesn't do anything.

I'm still fairly new to using animations in Unity, and I was wondering if anyone could help me solve this little problem. Many thanks in advance.


Animator

alt text

Transition any state - sword attack

alt text

Transition sword attack - idle

alt text

Animation

alt text

Code used

 if (anim.GetBool("Sword")) 
 {
     anim.SetBool("Sword", false);
 }
 
 if (Input.GetKeyUp (KeyCode.Mouse0)) 
 {
     anim.SetBool("Sword", true);
 }



[EDIT]

Extra information:

  • I put a Debug.Log in the statement where it sets the Sword bool to true, which does show up when I press the button, but I don't see the "Sword" checkbox in the animator being ticked on when I do so

  • The code for this animation is in the same script as the one in which I control the player's jumping and walking animations, although these two use different values (a bool called "On Ground" and a float called "Speed") to control the animations

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

Answer by zohaib817 · Jul 10, 2015 at 08:34 AM

if (anim.GetBool("Sword")) { anim.SetBool("Sword", false); }

if (Input.GetKeyUp (KeyCode.Mouse0)) { anim.SetBool("Sword", true); }

As you are using in update method, when you start the animation " anim.SetBool("Sword", true);" same time in update its getting false which will not work like that. Add a public method to make it false.

public void swordFalse() { anim.SetBool("Sword", false); }

call this function on the end of the frame to let your attack animation to complete.

secondly uncheck the fixed duration and "has exit time" options. hope it will work.

Comment
Add comment · Show 3 · 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 Sugar Mommy · Jul 10, 2015 at 12:30 PM 0
Share

Unfortunately it still doesn't seem to work :(

I've made the method like you suggested, removed the previous bit of code and ins$$anonymous$$d called the method at the very end of the Update function.

I've unticked the "Fixed Duration" and "Has Exit Time" options on both the transition to and from the sword attack animation.

However, there's still no sword showing up... I've edited the main post with some extra information that may help. Either way, thank you so much for taking the time to look into this.

avatar image zohaib817 · Jul 10, 2015 at 06:44 PM 0
Share

"I put a Debug.Log in the statement where it sets the Sword bool to true, which does show up when I press the button, but I don't see the "Sword" checkbox in the animator being ticked on when I do so"

beacuse you are setting the animation false in update, update make the animation rapidly false so that you can not see its bool true in animator, make a function out side the update function that is

public void swordFalse() { anim.SetBool("Sword", false); }

animation events add an event like this on last frame in your animation and call this function to make it false. not in update method,

avatar image Sugar Mommy · Jul 10, 2015 at 07:07 PM 0
Share

Whoops, my bad. But thanks for responding again!

I've changed the script so that the SwordFalse method isn't actually called anywhere in it, except on the very last frame of the animation.

Now there's still a problem though:

As soon as I press the key which is supposed to start the animation, the player's walking animation suddenly stops. The "Sword" bool does get set to true, but it looks like the attack animation doesn't actually start and is stuck somehow.

Here's a screenshot of what it looks like once I press the key:

alt text

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

22 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

Related Questions

2D Animation does not start 1 Answer

Handling animation unity 4.3 2d game 1 Answer

Animator problem with multiple sprites and one controller 0 Answers

Animator State Freezing 0 Answers

How to optimize 2D Animations created with 2D Animation packege? (v3.1.1) 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