• 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 prazzoo · Oct 25, 2017 at 06:59 AM · animator2d animation

Unity 2D animation Plays only once. Not working When triggered the second time

I had been reading many posts with similar problems and tried all suggested approaches for the last 2 days but still could not get my scenario to work. Therefore, I am posting my question with as much details as possible. The use case is shown in attached file.

The ‘ball’ game object is inside the “BallContainer” parent object. I have 3 animations: Idle, BallStraight and Shot1. Idle is the start position. When “Ball” button is clicked, I play the BallStraight Animation. The animation is simply a position transition. I just set the trigger for the animation to play.

 anim.SetTrigger("BallStraight");

The animation does not work when “Apply Root Motion” in the Animator component is Checked. So I un-checked it. Then the animation works. The ball moves. While the ball moves, button “Shot” is clicked. Then the ball should move to the right from its current position. To achieve this I used parent object position. (the script of code below is attached to the “Ball” object)

  void Start () 
     {
         anim = GetComponent<Animator>();
         defaultBallPosition = transform.position;
         defaultParentPosition = transform.parent.position;
     }

 public void AnimateBallMovement()
 {
     transform.parent.position = transform.position;
     transform.localPosition = Vector3.zero;
 
     anim.SetTrigger(“Shot1”);
 }

Now the ball moves right. When it reaches the boundary (which is a trigger collider), I have to move the ball back to “Idle” position. Since the “Idle” animation has no position coordinates defined, I have to set the position manually and change the animation state. Here I set it as follows.

  ![public void ResetBallPosition()
     {
         transform.parent.position = defaultParentPosition;
         transform.localPosition = Vector3.zero;
         anim.SetTrigger("Idle");
     }][1]

  

The ball moves back to its original position. All works as expected to this point. Now when I click the “Ball” Button again, it does not play the “BallStraight” animation. Everything freezes. When I check the Animator window, I see the state has moved to “BallStraight” but frozen there. However, I never saw the ball move at all. It only plays once. I just want the state to reset, and continue playing the steps. What am I doing wrong? Is the animation somehow changing into “applyRootMotion=true” implicitly? Because that is the same behavior I see when I check the “Apply Root Motion” check box in Animator component in the start.

[1]: /storage/temp/104400-problem.png

problem.png (200.2 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

79 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

Related Questions

How to optimize 2D Animations created with 2D Animation packege? (v3.1.1) 0 Answers

How to Change a sprite mesh at a certain frame in an animation clip without script? 1 Answer

Problem with door animation 1 Answer

Attempting to change AnimatorController during Runtime 0 Answers

Can't modify the property changed by 2D animation via LateUpdate() in 1 frame 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