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 J-R-Wood · Jan 18, 2014 at 11:41 PM · javascriptshootingammo

Gun shooting and ammo problem

I tried to make my script shoot a projectile from the gun and then that would subtract a number from a GUI text and then have it where I could run over a game object and it would add ammo to my gun and add a number to the GUI text. could someone tell me what part I need to fix.

 #pragma strict
 var prefabBullet:Transform;
 var shootForce:float;
 var shots : int = 0;
 var maxShots : int = 8;
 var shootSound : AudioClip;
 var ammoAmount : int = 50;
 public var ammo : int = 0;
 public var GUIammo : GUIText;
  
 
 function Update()
 {
     if(Input.GetButtonDown("Fire1") && shots < maxShots)
     {
         var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);
         instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
         audio.PlayOneShot(shootSound);
         shots++;
     }
 }
     
 function Update()
 {
 function OnTriggerStay(Collider);
 {
         if (tag == "ammo") 
         {
         Destroy(gameObject); 
         ammo += 1;
         shots -= 1;
         GUIammo.text = ammo.ToString();
         }
     }
     }
 
Comment
Add comment · Show 7
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 $$anonymous$$ · Jan 18, 2014 at 11:44 PM 0
Share

Nothing in compile error?

avatar image J-R-Wood $$anonymous$$ · Jan 18, 2014 at 11:47 PM 0
Share

no it just doesn't do the adding ammo thing I wanted, sorry I wasn't exact

avatar image $$anonymous$$ $$anonymous$$ · Jan 20, 2014 at 03:02 PM 0
Share

BTW sorry I put that on answer. I spend most of time unity+forums. Lol

avatar image $$anonymous$$ $$anonymous$$ · Jan 20, 2014 at 03:11 PM 0
Share

Well it depends. I just add physics and when the body falls over I call it dead and disable scripts.

avatar image Ibzy · Jan 20, 2014 at 03:07 PM 0
Share

Does the GameObject you run over get destroyed?

avatar image Ibzy · Jan 20, 2014 at 03:21 PM 0
Share

Have you tried if(Collider.CompareTags("ammo")) {}

avatar image J-R-Wood · Jan 20, 2014 at 09:05 PM 0
Share

yes it does get destroyed but i think it's because i added un needed things in that script i got it working now, thanks everyone!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Komak57 · Jan 20, 2014 at 10:08 PM

Usually safer in this case to use OnGUI() to render the clip information:

     void OnGUI() {
         
         Transform target = transform;
         while (target.parent != null) {
             target = target.parent;
             if (target.name == "Player") {
                 GUI.Box(new Rect(10,Screen.height-60,ClipSize*20+10,50), new Texture());
                 if (_ReloadSpeed > 0) {
                     GUI.Label(new Rect(15,Screen.height-60,70,30), ".../"+(TotalAmmo));
                 } else {
                     GUI.Label(new Rect(15,Screen.height-60,70,30), Ammo+"/"+(TotalAmmo));
                 }
                 for(int i = 0; i < Ammo; i++) {
                     GUI.DrawTexture(new Rect(i*20+15,Screen.height-30,20,20), ClipTexture);
                 }
                 return;
             }
         }
     }

Also, I've noticed that adding a script to the bullet to ray-cast and teleport has a much better collision performance. As far as ammo goes, you need to be more specific.

  1. You don't want to fire while reloading.

  2. You don't want to fire when your clip is empty.

  3. You don't want to reload when you're out of ammo.

Once you've jotted down all of your parameters, you can just throw looted ammo into the ammo variable, ensuring that there is a maximum if you so desire (most games do) etc etc.

As far as your looting problem, I have another script to act as an equipment manager. This script is initialized within another script attached to the parent object, though there's many ways to do this differently. Said object runs a 'void OnTriggerEnter(Collider c)' to decide if the player has come into auto-loot range, and then manages the ammo from the ground weapon to the players weapon. Get creative!

 public int TransferAmmo(GameObject obj) {
         GunMechanics loot = obj.GetComponent<GunMechanics>(); // firing, reloading, etc
         // Scan for item
         foreach(Transform child in handle) {
             if (child.name == obj.name) {
                 GunMechanics equip = child.GetComponent<GunMechanics>();
                 int AmmoToTransfer = loot.TotalAmmo;
                 // cap to full ammo
                 if (AmmoToTransfer + equip.TotalAmmo > equip.MaxAmmo)
                     AmmoToTransfer = equip.MaxAmmo - equip.TotalAmmo;
                 equip.TotalAmmo += AmmoToTransfer;
                 if (AmmoToTransfer > 0)
                     Debug.Log("Obtained "+AmmoToTransfer+"x "+equip.Bullet.name+" bullets.");
                 return AmmoToTransfer;
             }
         }
         return 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

20 People are following this question.

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

Related Questions

Repeatedly Getting Errors On An Ammo Script 3 Answers

In Game Animation 2 Answers

GUI Problem 2 Answers

Javascript code not working plz help 1 Answer

2D shooting problem please help! 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