Skip to content

Instantly share code, notes, and snippets.

@FUJI-bayashi
Created February 14, 2024 07:59
Show Gist options
  • Save FUJI-bayashi/e1d943cb2a430ad899c03ab1b545c465 to your computer and use it in GitHub Desktop.
Save FUJI-bayashi/e1d943cb2a430ad899c03ab1b545c465 to your computer and use it in GitHub Desktop.
using UnityEngine;
public class GunFireRateControl : MonoBehaviour
{
// Threshold for the number of shots to trigger the Dizzy effect
public int shotsThreshold = 5;
// Time window in seconds to count the shots
public float countingWindow = 2f;
// Counter for shots fired in the time window
private int shotsCounter = 0;
// Timer to track the counting window
private float timer = 0;
// Flag to check if Dizzy effect is currently active
private bool isDizzyActive = false;
void Update()
{
// Check if the countdown is active
if (timer > 0)
{
// Decrease the timer
timer -= Time.deltaTime;
}
else if (shotsCounter > 0)
{
// Reset the counter when the time window has passed without reaching the threshold
ResetShotsCounter();
}
// Example condition to check for gun firing, replace with your own condition
if (Input.GetButtonDown("Fire1"))
{
ProcessShotFired();
}
}
void ProcessShotFired()
{
if (timer <= 0)
{
// Start the countdown
timer = countingWindow;
}
// Increment the shots counter
shotsCounter++;
// Check if the shots threshold is exceeded
if (shotsCounter >= shotsThreshold)
{
TriggerDizzyEffect();
ResetShotsCounter();
}
}
void TriggerDizzyEffect()
{
// Apply the Dizzy effect here
if (!isDizzyActive)
{
isDizzyActive = true;
Debug.Log("Dizzy effect triggered!");
// Extend Dizzy effect if already active or apply it for the first time
}
// Reset or extend the Dizzy effect duration as needed
}
void ResetShotsCounter()
{
// Reset the counter and timer
shotsCounter = 0;
timer = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment