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);
}
}