Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Michael 12 · May 10, 2011 at 02:18 AM · pickupammo

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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

What is going on with this pickUp Script? 1 Answer

Need help with picking up items by typing "E" 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges