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. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Animation is inconsistent when firing..?

Discussion in 'Animation' started by Guga9, Jul 28, 2021.

  1. Guga9

    Guga9

    Joined:
    Oct 27, 2020
    Posts:
    4
    When the joystick value is 0.7 and above, it is constantly firing and animation plays at the same time. (Gun recoil) It's okay.
    However, when I fire only once, the animation plays 2 times. (until transition from firing animation to "idle" animation)

    In short, I'm trying to make a way of working like in a "shooter" game that fires rapidly when the joystick is pressed, works once when pressed once, and fires one by one when continuously pressed and released. (My attempt is for 2D)

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class pistolAttacking : MonoBehaviour
    7. {
    8.     public Transform firepoint;
    9.     public GameObject bulletPrefab;
    10.  
    11.     public float bulletForce;
    12.  
    13.     public Rigidbody2D rbp;
    14.     public Joystick AttackJoystick;
    15.  
    16.     bool isShooting;
    17.     public Animator pistolShoot;
    18.  
    19.     void Start()
    20.     {
    21.         isShooting = true;
    22.     }
    23.  
    24.  
    25.     void Update()
    26.     {
    27.         if (Mathf.Abs(AttackJoystick.Horizontal) > 0.1f || Mathf.Abs(AttackJoystick.Vertical) > 0.1f)
    28.         {
    29.             float x = AttackJoystick.Horizontal;
    30.             float y = AttackJoystick.Vertical;
    31.             float z = Mathf.Atan2(y, x) * Mathf.Rad2Deg;
    32.             rbp.transform.eulerAngles = new Vector3(0, 0, z);                
    33.         }
    34.  
    35.     }
    36.  
    37.  
    38.     void FixedUpdate()
    39.     {
    40.         if (Mathf.Abs(AttackJoystick.Horizontal) > 0.7f || Mathf.Abs(AttackJoystick.Vertical) > 0.7f)
    41.         {
    42.             pistolShoot.SetBool("pistolShooting", true);
    43.             if (isShooting)
    44.             {
    45.                 StartCoroutine(Shoot());
    46.                 isShooting = false;
    47.             }
    48.         }
    49.         else
    50.         {
    51.             pistolShoot.SetBool("pistolShooting", false);
    52.         }
    53.     }
    54.  
    55.     IEnumerator Shoot()
    56.     {
    57.         GameObject bullet = Instantiate(bulletPrefab, firepoint.position, firepoint.rotation);
    58.         Rigidbody2D rbb = bullet.GetComponent<Rigidbody2D>();
    59.         rbb.AddForce(firepoint.up * bulletForce, ForceMode2D.Impulse);
    60.  
    61.         yield return new WaitForSeconds(0.3f);
    62.    
    63.         isShooting = true;
    64.     }
    65.  
    66. }
    67.  
    68.  
     
unityunity