Search Unity

Question Playing weapon attack animation

Discussion in 'Animation' started by shawnaj98, Nov 28, 2021.

  1. shawnaj98

    shawnaj98

    Joined:
    Oct 15, 2019
    Posts:
    5
    Hi there, I am very new to Unity and working on a game for my capstone project. I have an Item script which lets me interact with items, grab and drop them, and also examine. I also have a PlayerCombat script which controls my attacks and attack animations. But I have worked in the animator and also tried to sort out how I could include that animation once the character picks up the bat in the item script.
    So basically, I just need the bat to follow my hand when attacking because it stays in the frozen default state once I pick it up and press attack.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerCombat : MonoBehaviour {
    4.  
    5.     public Transform attackPoint;
    6.     public LayerMask enemyLayers;
    7.     public Animator animator;
    8.  
    9.     public float attackRange = 0.5f;
    10.     public int attackDamage = 5;
    11.  
    12.     public float attackRate = 2f;
    13.     float nextAttackTime = 0f;
    14.  
    15.  
    16.     void Update()
    17.     {
    18.         if (Time.time >= nextAttackTime)
    19.         {
    20.  
    21.             if (Input.GetKeyDown(KeyCode.Mouse0))
    22.             {
    23.                 Attack();
    24.                 nextAttackTime = Time.time + 1f / attackRate;
    25.             }
    26.         }
    27.     }
    28.  
    29.     void Attack()
    30.     {
    31.         //Play an attack animation
    32.         animator.SetTrigger("Attack");
    33.         //Detect enemies in range of attack
    34.         Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);
    35.  
    36.         //Damage them
    37.         foreach (Collider2D enemy in hitEnemies)
    38.         {
    39.             enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
    40.             Debug.Log("Enemy damage");
    41.             AudioManager.instance.PlaySFX("playerHit");
    42.         }
    43.  
    44.     }
    45.     void OnDrawGizmosSelected()
    46.     {
    47.  
    48.         if (attackPoint == null)
    49.             return;
    50.         Gizmos.DrawWireSphere(attackPoint.position, attackRange);
    51.     }
    52.  
    53.     void Die()
    54.     {
    55.         GetComponent<Collider2D>().enabled = false;
    56.         this.enabled = false;
    57.     }
    58. }
    59.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5. using UnityEngine.Events;
    6.  
    7. [RequireComponent(typeof(BoxCollider2D))]
    8.     public class Item : MonoBehaviour
    9. {
    10.     public enum InteractionType {NONE, PickUp,Examine,GrabDrop}
    11.     public enum ItemType { NONE, Static, Consumables }
    12.     [Header("Attributes")]
    13.     public InteractionType interactType;
    14.     public ItemType type;
    15.     [Header("Examine")]
    16.     public string descriptionText;
    17.     [Header("Custom Events")]
    18.     public Sprite image;
    19.     public UnityEvent customEvent;
    20.     public UnityEvent consumeEvent;
    21.  
    22.     void Start()
    23.     {
    24.         m_anim = GetComponent<Animation>();
    25.     }
    26.     private void Reset()
    27.     {
    28.         GetComponent<Collider2D>().isTrigger = true;
    29.         gameObject.layer = 13;
    30.     }
    31.  
    32.     public void Interact()
    33.     {
    34.         switch (interactType)
    35.         {
    36.             case InteractionType.PickUp:
    37.                 //Add the object to the PickedUpItems list
    38.                 FindObjectOfType<InventorySystem>().PickUp(gameObject);
    39.                 //Disable
    40.                 gameObject.SetActive(false);
    41.                 break;
    42.             case InteractionType.Examine:
    43.                 //Call the Examine item in the interaction system
    44.                 FindObjectOfType<InteractionSystem>().ExamineItem(this);
    45.                 break;
    46.             case InteractionType.GrabDrop:
    47.                 //Grab interaction
    48.                 FindObjectOfType<InteractionSystem>().GrabDrop();
    49.                 break;
    50.             default:
    51.                 Debug.Log("NULL ITEM");
    52.                 break;
    53.         }
    54.  
    55.         //Invoke (call) the custom event(s)
    56.         customEvent.Invoke();
    57.     }
    58. }