Question by
MarineRec · May 01, 2019 at 09:25 AM ·
c#gun scriptfire rate
How do i make my gun have a fire rate
I've seen many tutorials on this and people getting helped but no scripts ever work for me. I would like to have a fire rate but nothing works. Please help me.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FullAutoBullet : MonoBehaviour {
//Drag in the Bullet Emitter from the Component Inspector.
public GameObject Bullet_Emitter;
//Drag in the Bullet Prefab from the Component Inspector.
public GameObject Bullet;
//Enter the Speed of the Bullet from the Component Inspector.
public float Bullet_Forward_Force;
void Update()
{
if (Input.GetMouseButton(0))
{
//The Bullet instantiation happens here.
GameObject Temporary_Bullet_Handler;
Temporary_Bullet_Handler = Instantiate(Bullet, Bullet_Emitter.transform.position, Bullet_Emitter.transform.rotation) as GameObject;
//Sometimes bullets may appear rotated incorrectly due to the way its pivot was set from the original modeling package.
//This is EASILY corrected here, you might have to rotate it from a different axis and or angle based on your particular mesh.
Temporary_Bullet_Handler.transform.Rotate(Vector3.right * 0);
//Retrieve the Rigidbody component from the instantiated Bullet and control it.
Rigidbody Temporary_RigidBody;
Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();
//Tell the bullet to be "pushed" forward by an amount set by Bullet_Forward_Force.
Temporary_RigidBody.AddForce(transform.right * Bullet_Forward_Force);
//Basic Clean Up, set the Bullets to self destruct after 10 Seconds, I am being VERY generous here, normally 3 seconds is plenty.
Destroy(Temporary_Bullet_Handler, 10.0f);
}
}
}
Comment
Hello.
I dont get why you do all this stuff only to instantiate an object...
And, to make a firerate, create a countdown, a cooldown. Every time the cooldown reach 0 it can shot again.