Hello everyone, i am making a gun script, so far it has bullet holes, sound, and ammo. But there is a few things that arnt working the way i want. In the script, i was wondering if there was a way that when my variable “clip” gets down to 0, then play another sound, but that doesnt work, and i was also wondering how to make the bullethole texture stop appearing when my variable “clip” gets down to zero.
One last question, is there a way to display a hitmarker when the raycast hits a certain tag? i have no idea how to use tags, i should though, but i cant.
Here is the Script (if anyone wants to use it, you just have to put it on your “MainCamera”)
var bulletTex : GameObject[]; // creates an array to use random textures of bullet holes
var power : int = 10; //provides power to our raycast that can affect our rigidbody components
var fireRate : float = 0.1;
var clip : float = 30;
var reloadTime : int = 2;
private var nextFire : float = 0.0;
var gunShot : AudioClip;
var dryShot : AudioClip;
function Update () {
Fire ();
}
function Fire () {
if(Input.GetButton("Fire1") && Time.time > nextFire) {
nextFire = Time.time + fireRate;
AudioSource.PlayClipAtPoint(gunShot, transform.position, 1);
ForceFire();
}
}
function ForceFire () {
var fwd = transform.TransformDirection(Vector3.forward); //casts our raycast in the forward direction
var hit : RaycastHit;
Debug.DrawRay(transform.position, fwd * 100, Color.green); //drays our raycast and gives it a green color and a length of 10 meters
if(Input.GetButton("Fire1") && Physics.Raycast(transform.position, fwd, hit, 10)){ //when we left click and our raycast hits something
Instantiate(bulletTex[Random.Range(0,3)], hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)); //then we'll instantiate a random bullet hole texture from our array and apply it where we click and adjust
// the position and rotation of textures to match the object being hit
if (hit.rigidbody !=null)
hit.rigidbody.AddForceAtPosition(fwd * power, hit.point); //applies a force to a rigidbody when we click on it. Multiples our forward raycast times our power variable at the position we click
}
if(Input.GetButton("Fire1")) {
Debug.Log ("It's getting to function Reload");
yield WaitForSeconds (fireRate);
clip = clip -1;
}
if(clip == 0) {
yield WaitForSeconds (reloadTime);
clip = 30;
if(Input.GetButtonDown("Fire1") && clip == 0); {
AudioSource.PlayClipAtPoint(dryShot, transform.position, 1);
if(Input.GetKey("r")) {
clip = 30;
}
}
}
}