Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to make better animations.

Discussion in 'Animation' started by PapoTheSnek, Nov 17, 2023.

  1. PapoTheSnek

    PapoTheSnek

    Joined:
    Jun 6, 2020
    Posts:
    1
    Hey guys could someone help me make this better? cant find any good first person melee combat tutorials and how to setup some nice animations for it. I want firs attack to be always unique lets say LightAttack1 and after that if player is clicking the mouse button play Lightattack2 and LightAttack3 (loop between them) but as u can see it looks weird idk what to do about it. what i tried -
    and here is my reference
    any help or advices or links for tutorials etc etc is much appreciated. its for a school project

    here is the code for light attacks
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.InputSystem;
    4.  
    5. public class LightAttacks : MonoBehaviour
    6. {
    7.  
    8.     [SerializeField] private WeaponData weaponData;
    9.     [SerializeField] private Animator animator;
    10.     [SerializeField] private AnimationClip KatanaAttack1;
    11.     [SerializeField] private AnimationClip KatanaAttack2;
    12.     [SerializeField] private AnimationClip KatanaAttack3;
    13.    
    14.  
    15.     private Blocking _blocking;
    16.  
    17.     public int comboCount = 0;
    18.     public float comboTimer = 0f;
    19.     public float timeBetweenCombos = 1f; // Time allowed between clicks
    20.  
    21.     public bool _isLightAttacking = false;
    22.  
    23.  
    24.     public bool isLightAttacking
    25.     {
    26.         get { return _isLightAttacking; }
    27.         set { _isLightAttacking = value; }
    28.     }
    29.  
    30.  
    31.     private void Awake()
    32.     {
    33.         _blocking = GetComponent<Blocking>();
    34.  
    35.     }
    36.  
    37.     void Update()
    38.     {
    39.         if (comboTimer > 0)
    40.         {
    41.             comboTimer -= Time.deltaTime;
    42.  
    43.             if (comboTimer <= 0)
    44.             {
    45.                 comboCount = 0; // Reset combo if time runs out
    46.             }
    47.         }
    48.     }
    49.  
    50.     public void LightAttackState(InputAction.CallbackContext context)
    51.     {
    52.         if (_blocking.IsBlocking) return;
    53.  
    54.         if (context.started && !_isLightAttacking)
    55.         {
    56.             comboTimer = timeBetweenCombos;
    57.  
    58.             if (comboCount == 0)
    59.             {
    60.                 comboCount = 1; // Start combo
    61.                 SetAnimationSpeed(KatanaAttack1, weaponData.attackSpeed);
    62.                 animator.SetTrigger("LightAttack1");
    63.             }
    64.             else
    65.             {
    66.                 // Toggle between 2 and 3
    67.                 comboCount = comboCount == 2 ? 3 : 2;
    68.  
    69.                 if (comboCount == 2)
    70.                 {
    71.                     SetAnimationSpeed(KatanaAttack2, weaponData.attackSpeed);
    72.                     animator.SetTrigger("LightAttack2");
    73.                 }
    74.                 else if (comboCount == 3)
    75.                 {
    76.                     SetAnimationSpeed(KatanaAttack3, weaponData.attackSpeed);
    77.                     animator.SetTrigger("LightAttack3");
    78.                 }
    79.             }
    80.         }
    81.     }
    82.  
    83.  
    84.  
    85.  
    86.     private void SetAnimationSpeed(AnimationClip clip, float chargeTime)
    87.     {
    88.         float animationLength = clip.length;
    89.         float requiredSpeed = animationLength / chargeTime;
    90.         animator.SetFloat("AttackSpeed", requiredSpeed);
    91.     }
    92. }