Input Cooldown | Input Delay | C# | Unity Game Engine


InputCooldown.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class InputCooldown : MonoBehaviour
{
    public float cooldownTime = 0f;
    private bool isCooldown = false;
 
    private void Update()
    {
        if (Input.GetMouseButton(0) && !isCooldown)
        {
            StartCoroutine(Cooldown());
            SpawnSphere();
        }
    }
 
    private IEnumerator Cooldown()
    {
        isCooldown = true;
        yield return new WaitForSeconds(cooldownTime);
        isCooldown = false;
    }
 
    private void SpawnSphere()
    {
        GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
        sphere.transform.position = Camera.main.ScreenToWorldPoint(mousePos);
        sphere.AddComponent<Rigidbody>();
        GameObject.Destroy(sphere, 5);
    }
}