How can i make the right choice of implementation of my shooting fire script ?
This is working but the way i'm doing it is wrong. I don't want to use not frame counts but rate of fire that is setup to be governed by the desired delay between shots in actual time.
I also want to make some bar i can move and change while the game is running the rate of fire. For example 10 bullets per second or 11 bullets per second or 200. I think the real one is 10 bullets per second but i want to be able to change it while the game is running. Something like this line: [Range(0f,20f)] public float bulletsPerSecond = 10f;
Not sure how to do it.
The first script is attached to the weapon that fire. The second script is attached to the bullet prefab i created. I have a Rigidbody component already on the weapon.
This is my script:
using UnityEngine;
using System.Collections;
public class Shooter : MonoBehaviour {
public GameObject bullet = null;
public float bulletSpeed = 500f;
private int frames = 0;
void Update ()
{
frames++;
if (frames % 10 == 0)
{
if (Input.GetKeyDown(KeyCode.G) || Input.GetKey(KeyCode.G))
{
GameObject shot = (GameObject)Instantiate(bullet, transform.position
+ (transform.forward * 2), transform.rotation);
Rigidbody _rigidbody = shot.AddComponent<Rigidbody>();
_rigidbody.AddForce(transform.forward * bulletSpeed);
}
}
}
}
And this is the script that destroy the bullets:
using UnityEngine;
using System.Collections;
public class Bullet : MonoBehaviour {
void OnCollisionEnter(Collision c)
{
Destroy(gameObject);
}
}
If you want it to be governed by actual time not framerate, use Time.deltaTime or something like that. Not sure this is what you're asking, but from what I understand you still need to know how to change this via script in-game, which I don't know how to do. Anyway, Time.deltaTime.
Answer by Bunny83 · Nov 26, 2016 at 03:23 PM
Replace this:
// [ ... ]
private int frames = 0;
void Update ()
{
frames++;
if (frames % 10 == 0)
{
if (Input.GetKeyDown(KeyCode.G) || Input.GetKey(KeyCode.G))
{
// [ ... ]
with this
// [ ... ]
public float coolDownTime = 0.2f; // 0.2 --> 5 shots per second
private float time = 0;
void Update ()
{
if (time > 0)
{
time -= Time.deltaTime;
}
else
{
if (Input.GetKey(KeyCode.G))
{
time += coolDownTime;
// [ ... ]
Note: using GetKeyDown and GetKey is redundant. GetKey will also return true the first frame the key is down. If you want to specify a firerate instead of the dellay between shots, just calculate this:
coolDownTime = 1f / fireRate; // fireRate is in "shots per seconds"
I tried to do it this way but i guess i'm wrong and have some questions:
public GameObject bullet = null; public float bulletSpeed = 500f; public float fireRate = 0.2f; public float coolDownTime = 1f; // 0.2 --> 5 shots per second private float time = 0;
void Update()
{
if (time > 0)
{
time -= Time.deltaTime;
}
else
{
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.G))
{
coolDownTime = 1f / fireRate;
time += coolDownTime;
GameObject shot = (GameObject)Instantiate(bullet, transform.position
+ (transform.forward * 2), transform.rotation);
Rigidbody _rigidbody = shot.AddComponent<Rigidbody>();
_rigidbody.AddForce(transform.forward * bulletSpeed);
}
}
}
Why when i change the value of coolDownTime while the game is running it's all the time get back 0.14 i tried to change it to 1 or to 7 but it's back to 0.14
What this do: transform.forward * 2 ? $$anonymous$$ake it to move twice faster ?
Did i use and put the calculation coolDownTime = 1f / fireRate; in the right place and the right way ?
What is more real or better to use the: To specify a firerate ins$$anonymous$$d of the dellay between shots or to use dellay between shots ?
I see now that i'm not using the time variable and the collDownTime in my old lines with the shot variable and the _rigidbody. What should i do ?
Your answer
Follow this Question
Related Questions
Can someone please help me find out what wrong with my code. 0 Answers
Enabling & Disabling Script via On Click 1 Answer
How can i give another name/number to the created Plane object name ? 0 Answers
How do I make somthing happen when the Player reaches a certain x, y, z position? 0 Answers
Reverse memcpy for NativeArray? 0 Answers