Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Max 4 · May 26, 2011 at 09:40 AM · raycastgunammo

Ammunition scripts

I know there are many scripts on making ammunition, but I have an odd firing script, and can't figure out how to implement ammo.

 var damage : float = 5;
 var gunshot : AudioClip;
 var muzzleflash : GameObject;
 
 
 function Update () {
     pistolflash = GameObject.FindWithTag("pistolflash");
     // raycast settings
     var direction = transform.TransformDirection(Vector3.forward);
     var hit : RaycastHit;
     var localOffset = transform.position;
     
     // check to see if user has clicked
     if(Input.GetButtonDown("Fire1"))
     {
         // if so, did we hit anything?
         if (Physics.Raycast (localOffset, direction, hit, 400)) 
         {
             // just so we can see the raycast
             Debug.DrawLine (localOffset, hit.point, Color.cyan);
             // print to console to see if we have fired
             print("we have fired!");
             // send damage report to object
             hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
         }
         AudioSource.PlayClipAtPoint(gunshot,transform.position);
         Instantiate (muzzleflash, pistolflash.transform.position, transform.rotation);
     }
 }

This is for a pistol. I have looked at the FPS tutorial but there is no way i could implement their system. Ultimately, I want to have a clip, ammo, and reload function. Any help is much appreciated.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by GesterX · May 26, 2011 at 02:25 PM

Have three int variables. One for the current clip, one for clip size, and one for ammo. Before you fire a shot just check if the clip variable is above 0 (at the same time as checking for the fire button).

To reload just have a function which finds the difference between the clipsize variable and the current clip. Then just take that amount away from ammo variable and add it to the current clip.

Comment
Add comment · Show 1 · Share
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
avatar image Max 4 · May 27, 2011 at 08:58 AM 0
Share

Err, I am trying to do that and it's difficult.

avatar image
0

Answer by WillTheThrill · Sep 25, 2012 at 05:52 AM

 var thirtyEightBullet : Rigidbody;
 var bulletSpeed = 10;
 var Magnum : AudioClip;
 //var magnumAmmo = 6;
 var canFire = true;
 var Empty : AudioClip;
 var revolverReload : AudioClip;
 var isReloading = false;
 var canShootAgain = true;
 var isActive = false;
 var icon : Texture2D;
 var reload : Texture2D;
 var isPrimary = false;
 var magnum_selected : Texture2D;
 var magnumMaxAmmo = 6;
 var magnumCurrentAmmo = 6;
 var shotsFired = 0;
 
 function Shooting() {
 if (isPrimary == true)
 if (isActive == true)
     if (canFire == true)
         {
         if(canShootAgain == true)
         {
             if (Input.GetButtonDown("Fire1"))
             {
             canShootAgain = false;
             // Instantiate the projectile at the position and rotation of this transform
             var clone : Rigidbody;
             clone = Instantiate(thirtyEightBullet, transform.position, transform.rotation);
             clone.velocity = transform.TransformDirection(( Vector3(Screen.width / 2,Screen.height / 2 - 50) * bulletSpeed) );
     // Give the cloned object an initial velocity along the current
     // object's Z axis
         //    clone.velocity = transform.TransformDirection (Vector3.forward * bulletSpeed);
             audio.PlayOneShot(Magnum);
             magnumCurrentAmmo -= 1;
             shotsFired +=1;
             yield WaitForSeconds (1.5);
             canShootAgain = true;
                 if(magnumCurrentAmmo == 0)
                 {
                 canFire = false;
                 }
                }
            }
     }
 
 if (canFire == false && isPrimary == true)
     {
     if (Input.GetButtonDown("Fire1"))
         {
         if(isReloading == true)
         {
         isReloading = true;
         }
             else
             {
             audio.PlayOneShot(Empty);
             yield WaitForSeconds (0.5);
             }         
            
         }
     }
 }    
 
 function Reload(){
 if(isPrimary == true)
 {
 if(isActive == true)
 {
     if(isReloading == false)
     {
         if(magnumMaxAmmo != 0)
         {
         if(Input.GetKeyDown("r"))
         {
         canFire = false;
         isReloading = true;
         audio.PlayOneShot(revolverReload);
         yield WaitForSeconds (5.5);
 //no ammo in clip, but more than 6 max
         if (magnumCurrentAmmo == 0 && magnumMaxAmmo >= 6)
             {
             magnumCurrentAmmo = 6;
             magnumMaxAmmo = magnumMaxAmmo - shotsFired;
             }
 //no ammo in clip, but less than 6 max
         if (magnumCurrentAmmo == 0 && magnumMaxAmmo < 6)
             {
             magnumCurrentAmmo = magnumMaxAmmo; 
             magnumMaxAmmo = 0;
             }            
 
 //some ammo in clip, but more than 6 max
         if (magnumCurrentAmmo < 6 && magnumCurrentAmmo > 1 && magnumMaxAmmo >= 6)
             {
             magnumCurrentAmmo = 6;
             magnumMaxAmmo = magnumMaxAmmo - shotsFired;
             }
 //some ammo in clip, but less than 6 max
         if (magnumCurrentAmmo < 6 && magnumCurrentAmmo > 1 && magnumMaxAmmo < 6)
             {
             if (magnumCurrentAmmo + magnumMaxAmmo == 6)
                 {
                 magnumCurrentAmmo = 6;
                 magnumMaxAmmo = 0;
                 }
             if ((magnumCurrentAmmo + magnumMaxAmmo) < 6)
                 {
                 magnumCurrentAmmo = magnumCurrentAmmo + magnumMaxAmmo;
                 magnumMaxAmmo = 0;
                 }        
             if ((magnumCurrentAmmo + magnumMaxAmmo) > 6)    
                 {
                 magnumCurrentAmmo = 6;
                 magnumMaxAmmo = magnumMaxAmmo - shotsFired;
                 }
             }
         shotsFired = 0;
         if(magnumCurrentAmmo == 0 && magnumMaxAmmo == 0)
             {
             canFire = false;
             }        
         shotsFired = 0;
         canFire = true;
         isReloading = false;
         }
         }
     }
 }
 }
 }
 function OnGUI()
 {
 
 GUI.Label (Rect (Screen.width / 2 , Screen.height - 20,150,100), "" + magnumCurrentAmmo + "/" + magnumMaxAmmo);
 if (isPrimary == true)
     {
     GUI.Box (Rect (Screen.width / 2, Screen.height - 50 ,170,50), icon);
     }
 else
     {
     GUI.contentColor = Color.gray;
     GUI.Box (Rect (Screen.width / 2, Screen.height - 50 ,170, 50), icon);
     }
 if (magnumCurrentAmmo == 0 && magnumMaxAmmo == 0)
     {
     GUI.contentColor = Color.red;
     GUI.Label (Rect (Screen.width / 2, Screen.height - 20,150,100), "" + magnumCurrentAmmo + "/" + magnumMaxAmmo);
     }
 if (magnumCurrentAmmo == 0 && isPrimary == true && isActive == true)    
     {
     if (Time.time % 1.1 < 1)
             GUI.Label (Rect (Screen.width / 2 - 75, Screen.height / 4, 200, 50), reload);
     }
 }
 
 function Update () {
 if (Input.GetKey("4"))
     {
     isActive = true;
     isPrimary = true;
     }
 else if(Input.GetKey("1") || Input.GetKey("2") || Input.GetKey("3") || Input.GetKey("5") || Input.GetKey("6")) 
     {
     isActive = false;
     isPrimary = false;
     }
 Shooting();
 Reload();
 }



I know this is long, but it's everything you need.

Comment
Add comment · Show 1 · Share
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
avatar image J-R-Wood · Jan 18, 2014 at 06:58 PM 0
Share

@WillTheThrill how would I make it so I can run over a game object and it would add ammo to my current gun

avatar image
0

Answer by alsharefeeee · Mar 16, 2014 at 08:53 AM

This is only the reload function, and I hope you find it useful:

function Reload(){

if(Ammo > 0 && MAG < 30) // MAG is the gun magazine or bullets in the gun.

{

animation.Play("reload");

Ammo = MAG + Ammo;

 if(Ammo >= 30)

{

 MAG = 30;

Ammo = Ammo - 30;

    }

    else

{

    MAG = Ammo;
    Ammo = 0;
    animation.Play("reload");

      }
  }

}

Comment
Add comment · Share
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
avatar image
0

Answer by LittleRainGames · Apr 04, 2016 at 09:02 AM

public int augAmmo = 30; public int augAmmoDif; public int augAmmoMax = 900; public int augClip = 30; public int augClipSize = 30; public int augClipDif;

void Reload() { if (augAmmo >= 1 && augClip <= 30) { if (Input.GetButtonDown("Reload")) { augAnim.SetBool("Reloading", true); augSource.PlayOneShot(augReload, 1f); } } }

     public void reloaded()
     {
         augAnim.SetBool("Reloading", false);


         if (augAmmo <= augClipSize)
         {
             augClipDif = (augClipSize - augClip);
             if(augAmmo <= augClipDif)
             {
                 augAmmoDif = (augClipDif - augAmmo);
                 augClip += augAmmoDif;
                 augAmmo = 0;
             }
             else
             {
                 augClip += augClipDif;
                 augAmmo -= augClipDif;
             }
             
         }
         else
         {
             augClipDif = (augClipSize - augClip);
             augAmmo = (augAmmo - augClipDif);
             augClip = augClipSize;
         }

         if (augAmmo <= 0)
         {
             augAmmo = 0;
         }
     }
Comment
Add comment · Share
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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

musket gun script is not working 3 Answers

How to send a raycast to an object to give me ammo? 0 Answers

How do I create bullet hole for my gun? 0 Answers

Creating a Ray Gun 1 Answer

Controlled Automatic Fire Rate (RayCasting) 2 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