Hello, friends! N00b coder, but looking to solve a problem I am having. My ideal is that when a player clicks on the screen, the player object (a hand) plays the appropriate animation. Once the animation is done, it resumes motion. The issue is re-initiating motion once it has stopped. The code works to make the hand stop when the button is clicked, but it doesn't resume once the animation finishes. Ideas? Relevant code below. Code (CSharp): { public float moveSpeed; public Rigidbody2D handBody; private readonly bool flipX; private SpriteRenderer hand; public Animator playerHand; private int handCatcher; private bool canMove; void Start() { handBody = GetComponent<Rigidbody2D>(); hand = GetComponent<SpriteRenderer>(); playerHand = GetComponent<Animator>(); canMove = true; } void Update() { //Hand functions if (Input.GetKeyDown(KeyCode.Mouse0)) { canMove = false; switch (handCatcher) { case (0): playerHand.SetBool("handCatch", true); break; case (1): playerHand.SetBool("cageOpen", true); break; case (3): playerHand.SetBool("handMiss", true); break; } } else { playerHand.SetBool("handCatch", false); playerHand.SetBool("cageOpen", false); playerHand.SetBool("handMiss", false); canMove = true; //This seems to make the hand move regarldess of the state of the above 'if'. } //What happens when missing if (handCatcher == 2 && Input.GetKeyDown(KeyCode.Mouse0)) { canMove = false; playerHand.SetBool("handMiss", true); } else { playerHand.SetBool("handMiss", false); canMove = true; //This seems to make the hand move regarldess of the state of the above 'if'. } //Need to figure a good way to trigger this true. //Right now, adding true to else statements makes it ALWAYS true, and I don't want that. Movement should be locked when animation plays. if (canMove == true) { transform.position = Vector2.Lerp(transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition), moveSpeed); Vector2 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position; handBody.position = new Vector2(handBody.position.x, handBody.position.y); Vector2 mousePosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y); difference.Normalize(); } Flipping(); } //Trigger for hand action switch void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.tag == "Enemy") { handCatcher = 0; Debug.Log("I am touching a hero"); } else if (collision.gameObject.tag == "Cage") { handCatcher = 1; } else if (collision.gameObject.tag != "Enemy" || collision.gameObject.tag != "Cage") { handCatcher = 2; } } void OnTriggerExit2D(Collider2D collision) { handCatcher = 2; } private void Flipping() { Vector2 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position; //Hand Flipping if (difference.x >= 0) { hand.flipX = true; } if (difference.x < 0) { hand.flipX = false; } } }
The problem is canMove will be set to true the very next frame after Input.GetKeyDown returns false. You need to use an animation event to get a callback from the animation system so that you know the animation has finished and can resume whatever you want to resume: https://docs.unity3d.com/Manual/animeditor-AnimationEvents.html
That did the trick. You're awesome! Just need to spice it in for all my events. This should actually help clean up some of the other functionality I need for the player object too.