Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Stopping Movement

Discussion in 'Scripting' started by masterpug13, Apr 16, 2019.

  1. masterpug13

    masterpug13

    Joined:
    Mar 31, 2019
    Posts:
    19
    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):
    1. {
    2.     public float moveSpeed;
    3.     public Rigidbody2D handBody;
    4.     private readonly bool flipX;
    5.     private SpriteRenderer hand;
    6.     public Animator playerHand;
    7.     private int handCatcher;
    8.     private bool canMove;
    9.  
    10.     void Start()
    11.     {
    12.         handBody = GetComponent<Rigidbody2D>();
    13.         hand = GetComponent<SpriteRenderer>();
    14.         playerHand = GetComponent<Animator>();
    15.         canMove = true;
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         //Hand functions
    21.         if (Input.GetKeyDown(KeyCode.Mouse0))
    22.         {
    23.             canMove = false;
    24.  
    25.             switch (handCatcher)
    26.             {
    27.                 case (0):
    28.                     playerHand.SetBool("handCatch", true);
    29.                     break;
    30.  
    31.                 case (1):
    32.                     playerHand.SetBool("cageOpen", true);
    33.                     break;
    34.  
    35.                 case (3):
    36.                     playerHand.SetBool("handMiss", true);
    37.                     break;
    38.             }
    39.         }
    40.  
    41.         else
    42.         {
    43.             playerHand.SetBool("handCatch", false);
    44.             playerHand.SetBool("cageOpen", false);
    45.             playerHand.SetBool("handMiss", false);
    46.             canMove = true; //This seems to make the hand move regarldess of the state of the above 'if'.
    47.         }
    48.  
    49.         //What happens when missing
    50.         if (handCatcher == 2 && Input.GetKeyDown(KeyCode.Mouse0))
    51.         {
    52.             canMove = false;
    53.             playerHand.SetBool("handMiss", true);
    54.         }
    55.  
    56.         else
    57.         {
    58.             playerHand.SetBool("handMiss", false);
    59.             canMove = true; //This seems to make the hand move regarldess of the state of the above 'if'.
    60.         }
    61.  
    62.         //Need to figure a good way to trigger this true.
    63.         //Right now, adding true to else statements makes it ALWAYS true, and I don't want that. Movement should be locked when animation plays.
    64.         if (canMove == true)
    65.         {
    66.             transform.position = Vector2.Lerp(transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition), moveSpeed);
    67.             Vector2 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    68.  
    69.             handBody.position = new Vector2(handBody.position.x, handBody.position.y);
    70.             Vector2 mousePosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
    71.  
    72.             difference.Normalize();
    73.         }
    74.  
    75.         Flipping();
    76.  
    77.     }
    78.    
    79.     //Trigger for hand action switch
    80.     void OnTriggerEnter2D(Collider2D collision)
    81.     {
    82.         if (collision.gameObject.tag == "Enemy")
    83.         {
    84.             handCatcher = 0;
    85.             Debug.Log("I am touching a hero");
    86.         }
    87.         else if (collision.gameObject.tag == "Cage")
    88.         {
    89.             handCatcher = 1;
    90.         }
    91.         else if (collision.gameObject.tag != "Enemy" || collision.gameObject.tag != "Cage")
    92.         {
    93.             handCatcher = 2;
    94.         }
    95.     }
    96.  
    97.     void OnTriggerExit2D(Collider2D collision)
    98.     {
    99.         handCatcher = 2;
    100.     }
    101.  
    102.     private void Flipping()
    103.     {
    104.         Vector2 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    105.         //Hand Flipping
    106.         if (difference.x >= 0)
    107.         {
    108.             hand.flipX = true;
    109.         }
    110.         if (difference.x < 0)
    111.         {
    112.             hand.flipX = false;
    113.         }
    114.     }
    115. }
    116.  
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    2,093
    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
     
    masterpug13 likes this.
  3. masterpug13

    masterpug13

    Joined:
    Mar 31, 2019
    Posts:
    19
    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.
     
    GroZZleR likes this.