Skip to content

Instantly share code, notes, and snippets.

@GigaOrts
Last active March 27, 2024 16:45
Show Gist options
  • Save GigaOrts/9eeb2a8df42ebb92672cb587e4701d6d to your computer and use it in GitHub Desktop.
Save GigaOrts/9eeb2a8df42ebb92672cb587e4701d6d to your computer and use it in GitHub Desktop.
using UnityEngine;
public class ComboAttack : MonoBehaviour
{
[SerializeField] private AnimationClip clip;
private int counter;
private int tempCounter;
private float chillDelay;
private float betweenComboDelay;
private float chillTimer;
private float betweenComboTimer;
private bool isAttack;
private Animator animator;
private void Awake()
{
betweenComboDelay = clip.length;
chillDelay = clip.length * 2;
animator = GetComponent<Animator>();
}
private void Update()
{
ChillCountdown();
bool CanAttack = counter < 3;
if (Input.GetKeyDown(KeyCode.Space) && CanAttack && isAttack == false)
{
isAttack = true;
counter++;
chillTimer = 0;
animator.SetInteger("ComboCounter", counter);
}
else if (Input.GetKeyDown(KeyCode.Space) && isAttack && CanAttack)
{
tempCounter = counter;
tempCounter++;
chillTimer = 0;
}
}
private void ChillCountdown()
{
if (isAttack && betweenComboTimer >= betweenComboDelay)
{
if (tempCounter > 0)
{
animator.SetInteger("ComboCounter", tempCounter);
tempCounter = 0;
betweenComboTimer = 0;
counter++;
return;
}
}
else if(isAttack)
{
betweenComboTimer += Time.deltaTime;
}
if (isAttack && chillTimer >= chillDelay)
{
counter = 0;
isAttack = false;
animator.SetInteger("ComboCounter", counter);
}
else if (isAttack)
{
chillTimer += Time.deltaTime;
}
}
private void OnGUI()
{
GUILayout.Label($"ComboCounter: {counter}");
}
}
using UnityEngine;
public class ComboAttack : MonoBehaviour
{
[SerializeField] private AnimationClip clip;
private int counter;
private int tempCounter;
private bool isAttack;
private Animator animator;
private Timer resetComboTimer;
private Timer nextComboTimer;
private void Awake()
{
resetComboTimer = new Timer(clip.length * 1.2f, ResetState);
nextComboTimer = new Timer(clip.length, NextState);
animator = GetComponent<Animator>();
}
private void Update()
{
bool CanAttack = counter < 3;
if (Input.GetKeyDown(KeyCode.Space) && CanAttack && isAttack == false)
{
isAttack = true;
counter++;
resetComboTimer.Reset();
animator.SetInteger("ComboCounter", counter);
}
else if (Input.GetKeyDown(KeyCode.Space) && isAttack && CanAttack)
{
tempCounter = counter;
tempCounter++;
resetComboTimer.Reset();
}
if (isAttack && tempCounter > 0)
{
nextComboTimer.Countdown(Time.deltaTime);
}
else if (isAttack)
{
resetComboTimer.Countdown(Time.deltaTime);
}
}
private void NextState()
{
counter++;
animator.SetInteger("ComboCounter", tempCounter);
tempCounter = 0;
}
private void ResetState()
{
counter = 0;
animator.SetInteger("ComboCounter", counter);
isAttack = false;
}
private void OnGUI()
{
GUILayout.Label($"ComboCounter: {counter}");
GUILayout.Label($"Elapsed RESET: {resetComboTimer.ElapsedTime}");
GUILayout.Label($"Elapsed NEXT: {nextComboTimer.ElapsedTime}");
}
}
using System;
public class Timer
{
private float period;
public float ElapsedTime;
private Action action;
public Timer(float period, Action action)
{
this.period = period;
this.action = action;
}
public void Countdown(float deltaTime)
{
if (ElapsedTime <= period)
{
ElapsedTime += deltaTime;
}
else
{
action?.Invoke();
Reset();
}
}
public void Reset()
{
ElapsedTime = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment