Search Unity

Question No Attack animation shortly after MoveInut

Discussion in 'Animation' started by Smiutschi, Jan 24, 2022.

  1. Smiutschi

    Smiutschi

    Joined:
    Jan 13, 2022
    Posts:
    8
    Hey :) first of all, im not sure if this belong in this forum ( i hope so) i have a problem. I set up a character for a 2D platformer and everything works great except for one thing. If i press my ''AttackButton'' shortly after i give a MoveInput (for example ''D'' in my case), the enemy takes dmg but the Attack animation dont go off. This only happens if i press Attack really quick after a MoveInput in every other case my AttackAnimation works.

    did someone know how i can fix this? I tried Transitions from ''all States'' to ''Attack'' and from ''run'' to ''Attack''
    but it isnt working.
    Here are the two Scripts :)
    Thanks

    edit: the same thing happens if i pressing Attack a milisecond after releasing the movebutton.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerMovementtest : MonoBehaviour
    4. {
    5.     [Header("Components")]
    6.     private Rigidbody2D rb;
    7.     public float speed;
    8.     public float jumpForce;
    9.     private float moveInput;
    10.  
    11.     [Header("Layar Mask")]
    12.     private bool isGrounded;
    13.     public Transform feetPos;
    14.     public float checkRadius;
    15.     public LayerMask whatIsGround;
    16.  
    17.     [Header("Jump")]
    18.     private float jumpTimeCounter;
    19.     public float jumpTime;
    20.     private bool isJumping;
    21.  
    22.     [Header("fall physics")]
    23.     public float fallMultiplier;
    24.     public float lowJumpMultiplier;
    25.  
    26.  
    27.  
    28.  
    29.     //Animations
    30.     private Animator anim;
    31.  
    32.     //Gets Rigidbody and Animator
    33.     void Start()
    34.     {
    35.         anim = GetComponent<Animator>();
    36.         rb = GetComponent<Rigidbody2D>();
    37.     }
    38.  
    39.     //Moves player on x axis
    40.     void FixedUpdate()
    41.     {
    42.         moveInput = Input.GetAxisRaw("Horizontal");
    43.         rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
    44.      
    45.         // Animation
    46.         if (moveInput == 0)
    47.         {
    48.             anim.SetBool("isRunning", false);
    49.         }
    50.         else
    51.         {
    52.             anim.SetBool("isRunning", true);
    53.         }
    54.     }
    55.  
    56.  
    57.  
    58.     void Update()
    59.     {
    60.  
    61.  
    62.         isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
    63.  
    64.         //turn twords you go
    65.         if (moveInput > 0)
    66.         {
    67.             transform.eulerAngles = new Vector3(0, 0, 0);
    68.         }
    69.         else if (moveInput < 0)
    70.         {
    71.             transform.eulerAngles = new Vector3(0, 180, 0);
    72.         }
    73.  
    74.      
    75.         //cool jump fall
    76.         if (rb.velocity.y < 0)
    77.         {
    78.             rb.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
    79.         }
    80.         else if (rb.velocity.y > 0 && Input.GetKey(KeyCode.Space))
    81.         {
    82.             rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;
    83.         }
    84.  
    85.         //fixed double jump bug
    86.         if (Input.GetKeyUp(KeyCode.Space))
    87.         {
    88.             isJumping = false;
    89.         }
    90.  
    91.         //lets player jump
    92.         if (isGrounded == true && Input.GetKeyDown("space") && isJumping == false)
    93.         {
    94.             anim.SetTrigger("takeOf");
    95.             isJumping = true;
    96.             jumpTimeCounter = jumpTime;
    97.             rb.velocity = Vector2.up * jumpForce;
    98.         }
    99.  
    100.         if (isGrounded == true)
    101.         {
    102.             anim.SetBool("isJumping", false);
    103.  
    104.         }
    105.         else
    106.         {
    107.             anim.SetBool("isJumping", true);
    108.         }
    109.         //makes you jump higher when you hold down space
    110.         if (Input.GetKey(KeyCode.Space) && isJumping == true)
    111.         {
    112.  
    113.             if (jumpTimeCounter > 0)
    114.             {
    115.                 rb.velocity = Vector2.up * jumpForce;
    116.                 jumpTimeCounter -= Time.deltaTime;
    117.             }
    118.             else
    119.             {
    120.                 isJumping = false;
    121.  
    122.             }
    123.  
    124.  
    125.         }
    126.  
    127.     }
    128.  
    129. }
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerAttack : MonoBehaviour
    4. {
    5.     private Animator anim;
    6.     private float timeBtwAttack;
    7.     public float startTimeBtwAttack;
    8.  
    9.     public Transform attackLocation;
    10.     public float attackRange;
    11.     public LayerMask enemies;
    12.     public int damage;
    13.  
    14.  
    15.     private void Start()
    16.     {
    17.         anim = GetComponent<Animator>();
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.         if (timeBtwAttack <= 0)
    23.         {
    24.             if (Input.GetButtonDown("Fire1"))
    25.             {
    26.            
    27.                 anim.SetBool("isAttacking", true);
    28.                 Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackLocation.position, attackRange, enemies);
    29.  
    30.                 for (int i = 0; i < enemiesToDamage.Length; i++)
    31.                 {
    32.                     enemiesToDamage[i].GetComponent<Enemy>().TakeDamage(damage);
    33.                 }
    34.                 timeBtwAttack = startTimeBtwAttack;
    35.             }
    36.          
    37.         }
    38.         else
    39.         {
    40.             timeBtwAttack -= Time.deltaTime;
    41.             anim.SetBool("isAttacking", false);
    42.         }
    43.        
    44.      
    45.  
    46.     }
    47.  
    48.     private void OnDrawGizmosSelected()
    49.     {
    50.         Gizmos.color = Color.red;
    51.         Gizmos.DrawWireSphere(attackLocation.position, attackRange);
    52.     }
    53. }
     
    Last edited: Jan 24, 2022
  2. unity_2cdeity

    unity_2cdeity

    Joined:
    Dec 19, 2020
    Posts:
    17
    Before the idle animation is finished playing the attack has already ended, so it never transitioned.