Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 mathkhang · Sep 17, 2018 at 04:13 AM · animationcollideranimatorcollider2dcollision2d

Character Hit Animation play endlessly

hello guys, i want to play the hurt animation when my character hit collider, the animation start but my character Hit Animation play endlessly. I dont know how to make him back to previous state of animation, here my code

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;

public class PlayerControl : MonoBehaviour { private Vector2 Targetpos; public float Yincrement; public float speed; public float maxHeight; public float minHeight; public int health ; public GameObject effect; private Animator anim;

 // Use this for initialization
 private void Start()
 {

     anim = GetComponent<Animator>();
 }

 // Update is called once per frame
 void Update ()
 {


     if (health <= 0)
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
     }

     transform.position = Vector2.MoveTowards(transform.position, Targetpos, speed * Time.deltaTime);

     if (Input.GetKeyDown(KeyCode.UpArrow)&&transform.position.y<maxHeight)
     {
         Instantiate(effect, transform.position, Quaternion.identity);

         Targetpos = new Vector2(transform.position.x,transform.position.y + Yincrement);
     }
      if (Input.GetKeyDown(KeyCode.DownArrow) && transform.position.y > minHeight)
     {

         Instantiate(effect, transform.position, Quaternion.identity);

         Targetpos = new Vector2(transform.position.x, transform.position.y - Yincrement);


     }
    


 }
 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.CompareTag("Enemy"))
     {
         anim.SetBool("IsHurt",true);


     }
 }

 IEnumerator Delay()
 {
     yield return new WaitForSeconds(1.0f);
     anim.SetBool("IsHurt", false);
 }







}

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
Best Answer

Answer by teja6595 · Sep 17, 2018 at 07:05 AM

You need to disable the loop of that hurt Animation. Just click on the animation clip in the project and u will find some setting on the inspector in that uncheck the loop.

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 mathkhang · Sep 17, 2018 at 09:26 AM 0
Share

Thanks, i disable the animation loop but this time my character freeze at the end of the hurt animation, it can't go back to the previous state which as walking, can you tell me what i did wrong ._.

avatar image mathkhang · Sep 17, 2018 at 09:38 AM 0
Share

ok i solve the problem, i chang Setbool to SetTrigger and it works perfectly. thank you alot

avatar image
0

Answer by myzzie · Sep 17, 2018 at 06:38 AM

Turn off animation loop.

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 mathkhang · Sep 17, 2018 at 09:26 AM 0
Share

Thanks, i disable the animation loop but this time my character freeze at the end of the hurt animation, it can't go back to the previous state which as walking, can you tell me what i did wrong ._.

avatar image Casiell mathkhang · Sep 17, 2018 at 09:31 AM 0
Share

Sounds like you don't have a path from 'hurt' to 'walking'. You should use AnyState to transfer your animation states, it really helps to avoid cases like these

avatar image mathkhang · Sep 17, 2018 at 09:38 AM 0
Share

ok i solve the problem, i chang Setbool to SetTrigger and it works perfectly. thank you alot

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

261 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 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 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

OnTriggerEnter2D working, OnColliderEnter2D not working 1 Answer

OnTriggerStay2D Breaks When Adding AnimationController 1 Answer

Collision detection problem 0 Answers

HOW TO CALLBACK COLLIDER WITH NAME? 2 Answers

How do I detect which collider is being collided with? 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