Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to set "Movement" Bool true after animation has played.

Discussion in 'Scripting' started by ScaryFace, Apr 4, 2020.

  1. ScaryFace

    ScaryFace

    Joined:
    Aug 7, 2015
    Posts:
    12
    Hey Team

    Back again. so i have spent the day looking for a way to stop my players Velocity while the attack animation is playing.

    There are a lot of people who ask this question but I don't understand the solutions given, and i don't know how to use Enumerators.

    So my idea was "while" the animation was playing, set bool canMove to false then back to true when the animation ends.

    but its not working out so well...I am obviously missing something here.
    Any help would be appreciated.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerControllerScript : MonoBehaviour
    6. {
    7.     Animator animator;
    8.     Rigidbody2D rb2d;
    9.     SpriteRenderer spriteRenderer;
    10.  
    11.     bool isGrounded;
    12.     bool canMove = true;
    13.     bool hasJumped = false;
    14.     int weaponSwingCount = 0;
    15.     public float velocity = 1f;
    16.     public float jumpVelocity = 5f;
    17.     public float swingVelocity = 0.5f;
    18.     public float attackRate = 2f;
    19.  
    20.     float nextAttackTime = 0f;
    21.     float axisHorizontal = 0f;
    22.     float moveHorizontal = 0f;
    23.  
    24.  
    25.     [SerializeField]
    26.     Transform groundCheck;
    27.     // Start is called before the first frame update
    28.     void Start()
    29.     {
    30.         animator = GetComponent<Animator>();
    31.         rb2d = GetComponent<Rigidbody2D>();
    32.         spriteRenderer = GetComponent<SpriteRenderer>();
    33.     }
    34.  
    35.  
    36.  
    37.  
    38.     // Update is called once per frame
    39.     void FixedUpdate()
    40.     {
    41.         axisHorizontal = Input.GetAxisRaw("Horizontal");//collect axis also used for Flip X Calc
    42.         moveHorizontal = DigitizeAnalog(axisHorizontal);
    43.  
    44.         if (canMove)
    45.         {
    46.             rb2d.velocity = new Vector2(moveHorizontal, rb2d.velocity.y); // change in velocity
    47.             animator.SetFloat("Speed", Mathf.Abs(moveHorizontal)); //set parameter speed to 1 or 0
    48.         }
    49.  
    50.  
    51.         if (GroundedCheck())
    52.         {
    53.             if (Input.GetButtonDown("Jump"))
    54.             {
    55.                 rb2d.AddForce(transform.up * jumpVelocity, ForceMode2D.Impulse);
    56.                 animator.SetTrigger("Jump");
    57.                 hasJumped = true;
    58.             }
    59.  
    60.             if(Input.GetButtonDown("Fire3"))
    61.             {
    62.                 WeaponDrawn();
    63.             }
    64.  
    65.  
    66.             if (animator.GetBool("IsDrawn"))
    67.             {
    68.                 if (Time.time >= nextAttackTime)
    69.                 {
    70.                     if (Input.GetButtonDown("Fire1") && weaponSwingCount <= 3)
    71.                     {
    72.                         canMove = false;
    73.                         AttackGround();
    74.                         nextAttackTime = Time.time + 1 / attackRate;
    75.                         if(animator.GetCurrentAnimatorStateInfo(0).IsName("Player_Attack1"))
    76.                         {
    77.                             canMove = true;
    78.                         }
    79.                     }
    80.                     else if (weaponSwingCount == 3)
    81.                     {
    82.                         weaponSwingCount = 0;
    83.                         nextAttackTime = 0;
    84.                     }
    85.                 }
    86.             }
    87.         }else if(!GroundedCheck())
    88.         {
    89.             if(Input.GetButtonDown("Jump") && hasJumped)
    90.             {
    91.                 rb2d.AddForce(transform.up * jumpVelocity, ForceMode2D.Impulse);
    92.                 animator.SetTrigger("DoubleJump");
    93.                 hasJumped = false;
    94.             }
    95.         }
    96.  
    97.         FlipX(spriteRenderer, axisHorizontal);
    98.     }  
    99.    
    100.  
    101.     void WeaponDrawn()
    102.     {
    103.         if (!animator.GetBool("IsDrawn"))
    104.         {
    105.             animator.SetTrigger("DrawSword");
    106.             animator.SetBool("IsDrawn", true);
    107.         }
    108.         else if (animator.GetBool("IsDrawn"))
    109.         {
    110.             animator.SetBool("IsDrawn", false);
    111.         }
    112.     }
    113.    
    114.     void AttackGround()
    115.     {    
    116.             ++weaponSwingCount;
    117.             animator.SetInteger("Swing", weaponSwingCount);
    118.             animator.SetTrigger("SwingTrigger");
    119.     }
    120.  
    121.     bool GroundedCheck()
    122.     {
    123.         if (Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Player")))
    124.         {
    125.             animator.SetBool("IsGrounded", true);
    126.             return true;
    127.         }
    128.         else
    129.         {      
    130.             animator.SetBool("IsGrounded", false);
    131.             return false;
    132.         }
    133.     }
    134.  
    135.     void FlipX(SpriteRenderer sr, float horizontal)
    136.     {
    137.         if (axisHorizontal < 0)
    138.         {
    139.             spriteRenderer.flipX = true;
    140.         }
    141.         if (axisHorizontal > 0)
    142.         {
    143.             spriteRenderer.flipX = false;
    144.         }
    145.     }
    146.  
    147.     int DigitizeAnalog(float axisMovement)//change input from controller to whole numbers
    148.     {
    149.         const float threshhold = 0.7f;
    150.         if (axisMovement > threshhold) return 1;
    151.         if (axisMovement < -threshhold) return -1;
    152.         return 0;
    153.     }
    154.  
    155. }
    156.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Hello again ScaryFace! I think what you want is something called an Animation Event. Here's the docs for it:

    https://docs.unity3d.com/Manual/script-AnimationWindowEvent.html

    The basic gist is you put markers in your animation timeline that you hook up to call a specific function.

    When the animation hits that marker, it can call a function in your script. You can use this for sounds, motion, particle system starts, etc.... just about anything.

    There's even some good video tutorials on animation events if you check around.
     
  3. ScaryFace

    ScaryFace

    Joined:
    Aug 7, 2015
    Posts:
    12
    Thanks again Sensei

    It wasn't that hard to figure out. the documentation did the trick.

    I created this dude, then set "int" from the event in the key frame of my choice and passed either one or zero and boom I has stop motion.



    Code (CSharp):
    1.     void StopMovement(int i)
    2.     {
    3.         if (i == 1)
    4.         {
    5.             canMove = false;
    6.         }
    7.         else if (i == 0)
    8.         {
    9.             canMove = true;
    10.         }
    11.     }
     
    Kurt-Dekker likes this.