Question by
juanmontescanovas · Jul 01, 2020 at 09:42 PM ·
c#script.shooting
Adda cooldown to a Shooting Script
I Have this simple scrip fot the player to shoot a prefab when clicking the "Disparo" button, in this case the left click. Now its broken because you can spam the click and shoot every second. I dont Know how to add a cooldown to this to make that you only can shoot every certain time.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class ProjectileShooter : MonoBehaviour
{
public AudioClip disparo;
GameObject prefab;
public float shootSpeed;
// Start is called before the first frame update
void Start()
{
prefab = Resources.Load("Projectile") as GameObject;
}
// Update is called once per frame
void Update()
{
if (CrossPlatformInputManager.GetButtonDown("Disparo"))
{
GameObject Projectile = Instantiate(prefab) as GameObject;
Projectile.transform.position = transform.position + Camera.main.transform.forward * 2;
Rigidbody rb = Projectile.GetComponent<Rigidbody>();
rb.velocity = Camera.main.transform.forward * shootSpeed;
Destroy(Projectile, 2.2f);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How can i make the right choice of implementation of my shooting fire script ? 1 Answer
Stuttering in build 0 Answers
How can I modify/change a prefab? 1 Answer
how i can jump? 0 Answers
Difference between GetComponent 2 Answers