- Home /
WaitForSeconds - Shooting Script
I Have a REALLY basic script in C# that basically spawn a bullet, and then move it. (Basically a shoot)
It doesn't matter if you click the mouse button as fast as you can, it will not shoot so fast, but it still is fast, and when i make the reloading system, it will be a chaos. So, i've been trying to put a delay in the script.
Example: THE BULLET IS SPAWNED. THE BULLET IS SHOOT. WAIT FOR 1.3 SECONDS ANOTHER BULLET IS SPAWNED ANOTHER BULLET IS SHOOT ETC...
But i can't get it to work! I cannot understand what to do! I already read the manual but it doesn't help a all, as (in my opinion), it's saying to me to create a script to make another script to work or something like that. I can't figure out what i need to do! Can someone explain me how can i do this? I would appreciate if anyone could complete the script =|
using UnityEngine; using System.Collections;
public class ShootDemo : MonoBehaviour { public Rigidbody projectile;
public float speed = 0;
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
}
}
}
What part are you struggling with? Ti$$anonymous$$g? Actually getting bullet to move?
There are a bunch of ways in which you could do this...
The ti$$anonymous$$g. I don't know how to put a waitforseconds in the script without getting any errors, or just doesn't work.
Answer by ishdagz · Jun 11, 2016 at 12:46 PM
The code below is from Unity documentation on using Time.time - it may be just what you are looking for. Try adding in your code to this or vice versa. It adds a simple countdown timer that resets a countdown before being allowed to fire again
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour {
public GameObject projectile;
public float fireRate = 0.5F;
private float nextFire = 0.0F;
void Update() {
if (Input.GetButton("Fire1") && Time.time > nextFire) {
nextFire = Time.time + fireRate;
GameObject clone = Instantiate(projectile, transform.position, transform.rotation) as GameObject;
}
}
}
It worked! Thanks!
I had to make a few changes in the codes but it's ok, it worked ^.^
Answer by KuR5 · Jun 11, 2016 at 06:18 AM
You can check the status of boolean variables that decides whether bullet can be fired or not.
Like, we have boolean variable named canFire with default value true. When bullet will be fired it is to be set to false. After decided time (in your case 1.3 secs) it will be set to true so bullet can be shoot again.
public float speed = 0;
bool _canFire=true;
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1") && _canFire)
{
Fire();
}
}
void Fire()
{
Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
_canFire=false;
StartCoroutine(WaitToEnableFire());
}
IEnumerator WaitToEnableFire()
{
yield return new WaitForSeconds(1.3f); //decided time to wait
_canFire=true;
}
Answer by Enyph-Games · Jun 11, 2016 at 08:47 AM
Untested, but it should work:
public float speed = 0;
float timer;
public float fireRate; // Update is called once per frame
void Update() { timer += Time.deltaTime; if (Input.GetButtonDown("Fire1") && timer >= fireRate) { timer = 0; Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody; instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed)); } }
Your answer
Follow this Question
Related Questions
how can I change a light with multiple triggers. ? 0 Answers
Simple panel questions 1 Answer
PrefabUtility doesn´t work 0 Answers
Beginner Question: How to get normals from a physics raycast using visual scripting? 0 Answers
Help with ontrigger enter and exit with UI Display appear and disappear 1 Answer