- Home /
How to make a reloading time because now i can shoot unlimited
Hello, I have a problem and i think is t not very hard but i can't fix it. Now i can shoot rockets but there is no limit, i can shoot unlimited and that not really realistic :) thanks in advance, Sander
Script:
using UnityEngine;
using System.Collections;
public class RocketSpawnSource : MonoBehaviour {
public GameObject RocketPrefab;
public static bool rocketShooted;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
Instantiate(RocketPrefab,transform.position,transform.rotation);
StartCoroutine("RocketShooted");
}
}
}
Answer by Landern · Oct 25, 2014 at 03:26 PM
You need a cool down of some sort.
using UnityEngine;
using System.Collections;
public class RocketSpawnSource : MonoBehaviour {
public GameObject RocketPrefab;
public static bool rocketShooted;
public int RocketCoolDown = 5; // Used to assign in editor and reset the rocketCoolDownCount.
private int rocketCoolDownCount;
// Use this for initialization
void Start () {
rocketCoolDownCount = RocketCoolDown; // initialize the Cool down count.
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
if (rocketCoolDownCount > 0)
{
rocketCoolDownCount--;
}
else
{
Instantiate(RocketPrefab,transform.position,transform.rotation);
StartCoroutine("RocketShooted");
}
}
}
}
In your RocketShooted method that you didn't include here, you need to reset the rocketCoolDownCount to RocketCoolDown.
// in your RocketShooted method, after shooting
rocketCoolDownCount = RocketCoolDown;
at first,i appreciate your helpthanks!
but we dont have a RocketShooted method and now i can shoot also unlimited, when i start the game and want to shoot the first rocket, i have to click 5 times and than the first rocket shoot, after that i can shoot unlimited such as in the beginning
Answer by bubzy · Oct 27, 2014 at 11:07 PM
float shootDelay = 0.2f;
float currentDelay;
void Start()
{
currentDelay = Time.time + shootDelay;
}
void Update () {
if (Input.GetMouseButtonDown(0))
{
if(Time.time > currentDelay)
{
Instantiate(RocketPrefab,transform.position,transform.rotation);
StartCoroutine("RocketShooted");
currentDelay = Time.time + shootDelay;
}
}
}
Your answer
Follow this Question
Related Questions
Shooting and reloading mechanism problem. 1 Answer
Shoot Script Reload Help 1 Answer
When reloading my gun sounds and the bullets glitch out. 2 Answers
gun wont reload 1 Answer
Problem With Bullet Prefab 2 Answers