- Home /
Help Needed with Shotgun script (SOLVED)
With some help from the Unity Scripting forum I was able to get the shotgun script to load Clips of ammo.
This is basically the FPS Tutorial Machine Gun Script that has been modified into a Shotgun script so that when you fire two shell you need to reload with two more shells.
var range = 70.0; var fireRate = 4.00; var force = 300.0; var damage = 10.0; var shellsLeft : int = 12; // How Many shells the player has var shells : int = 2; // How many Shells are loaded var clipSize = 2; // How many shells can we fit each time we reload
var reloadTime = 1.0; var reloading = false; var gunAnimation : Animation; var reloadSound : AudioClip; private var hitParticles : ParticleEmitter; var muzzleFlash : Renderer;
private var nextFireTime = 0.0; private var m_LastFrameShot = -1;
function Start () { hitParticles = GetComponentInChildren(ParticleEmitter);
// We don't want to emit particles all the time, only when we hit something.
if (hitParticles)
hitParticles.emit = false;
shells = clipSize;
}
function LateUpdate() { if(Input.GetButtonDown("Reload") && shellsLeft > 0) //set button to whatever you want Reload(); if (muzzleFlash) { // We shot this frame, enable the muzzle flash if (m_LastFrameShot == Time.frameCount) { muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360, Vector3.forward); muzzleFlash.enabled = true;
if (audio) {
if (!audio.isPlaying)
audio.Play();
audio.loop = true;
}
} else {
// We didn't, disable the muzzle flash
muzzleFlash.enabled = false;
enabled = false;
// Play sound
if (audio)
{
audio.loop = false;
}
}
}
}
function Fire () { if (shells == 0 && shellsLeft > 0 && reloading == false) Reload(); if (shells == 0 || reloading == true) return;
// If there is more than one bullet between the last and this frame
// Reset the nextFireTime
if (Time.time - fireRate > nextFireTime)
nextFireTime = Time.time - Time.deltaTime;
// Keep firing until we used up the fire time
while( nextFireTime < Time.time && shells != 0) {
FireOneShot();
nextFireTime += fireRate;
}
}
function FireOneShot () { var direction = transform.TransformDirection(Vector3.forward); var hit : RaycastHit; gunAnimation.Play("Shoot"); // Did we hit anything? if (Physics.Raycast (transform.position, direction, hit, range)) { // Apply a force to the rigidbody we hit if (hit.rigidbody) hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
// Place the particle system for spawing out of place where we hit the surface!
// And spawn a couple of particles
if (hitParticles) {
hitParticles.transform.position = hit.point;
hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
hitParticles.Emit();
}
// Send a damage message to the hit object
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
shells--;
// Register that we shot this frame,
// so that the LateUpdate function enabled the muzzleflash renderer for one frame
m_LastFrameShot = Time.frameCount;
enabled = true;
// Reload gun in reload Time
if (shells == 0)
Reload();
}
function Reload () { if(shellsLeft == 0) return; reloading = true; // Wait for reload time first - then add more bullets! yield WaitForSeconds(1); gunAnimation.Play("Reload"); audio.PlayOneShot (reloadSound); yield WaitForSeconds(reloadTime);
// We have a clip left reload
if (shellsLeft > clipSize) {
shellsLeft -= (clipSize - shells);
shells = clipSize;
}
else{
shells = shellsLeft;
shellsLeft = 0;
}
reloading = false;
}
function GetShellsLeft () { return shellsLeft; }
This is what goes on your Ammo pickup to get a refill of 12 clips or 24 shells: You just have to mod that into whatever you are doing for game ammo pickups.
var shells = 24;
else if (pickupType == PickupType.Shells) { var Shells : Shotgun = player.GetComponentInChildren(Shotgun); if (Shells) Shells.shellsLeft += shells;
Player JS And if you are using the GUI from the FPS Tutorial put this into the Update GUI function
// Update Shotgun gui
// Shotgun gui is simply drawn with a bullet counter text
if (shotgun) {
shotgunGUI.text = shotgun.GetShellsLeft().ToString();
}
NOTE: I figured I'd share this fix as it was such a pain in the you know where to get it solved. So if your like me and new to this stuff hopefully this will save you a lot of hair pulling trying to get a simple shotgun script.
Your answer
![](https://koobas.hobune.stream/wayback/20220613133112im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
eating candy, spitting it out. 1 Answer
ammo cache pickup 1 Answer
how do you pick up ammo by clip amount 1 Answer