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 /
  • Help Room /
avatar image
0
Question by ownerfate · Oct 31, 2013 at 10:41 PM · shootingammo

[CLOSED] {SOLVED} how could i add an ammo script to this?

hello again, this is a question that most might have seen more than once, but i have been trying to find the best way to top up ammo with this script.

 //shootScript.js
 
 var pelletPrefab : Rigidbody;
 
 var pelletSpeed : float = 150;
 
 var pelletCount : int = 5;
 
 var spreadFactor : float = 0.01;
 
 var fireRate : float = 0.5;
 
 var target : Transform;
 
 var MaxAmmo = 6;
 var CurrentAmmo = 6;
  var canFire = true;
 var canShootAgain = true;
  var shotsFired = 0;
  
  var CurrentAmmoGUI : GUIText;
 
 private var nextFire : float = 0.0;
 
 function Awake ()
  
 {
  
      if (!target)
      {
  
          target = GameObject.FindWithTag ("Player").transform;
      }
 }
 function Update() 
 
 {
 OnGUI(); 
   Shooting();
 }
  
 
 function   Shooting() {
 if (canFire == true)
  
  {
        if(canShootAgain == true)
 
     if(Input.GetButtonDown("Fire1") && Time.time > nextFire){
      canShootAgain = false;
 
         var pellet : Rigidbody;
 
         for(var i = 0; i<pelletCount; i++){
 
             var pelletRot = transform.rotation;
 
             pelletRot.x += Random.Range(-spreadFactor, spreadFactor);
 
             pelletRot.y += Random.Range(-spreadFactor, spreadFactor);
 
             pellet = Instantiate(pelletPrefab, transform.position, pelletRot);
 
             pellet.velocity = transform.forward*pelletSpeed;
             CurrentAmmo -= 1;
            shotsFired +=1;
            
            canShootAgain = true;
              if(CurrentAmmo == 0)
              {
              
              canFire = false;
              if(canFire == false){
              yield WaitForSeconds(0.2);
              audio.Play();
              }
 
         }
 
         nextFire = Time.time + fireRate;
 
     }
 
     
 
 }
 }
 }
 
 
 function OnGUI(){
 CurrentAmmoGUI.text = "" +CurrentAmmo.ToString(); //displays our current health status
 }

i'v tried a good deal of ways, but it just doesn't seem to work, when the gun runs out of ammo, i go to the ammo box, and try to Top up the ammo count, and nothing happens.

if anyone could point out what i'm doing wrong here, that would be helpful.

thank you

  • Ownerfate

extra details:

the ammo box does not have a tag , nor did the scripts refer to the item in any sense.

(i have even tried with tags and names, still didn't work)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ownerfate · Nov 10, 2013 at 12:54 AM

Never Mind, i figured it out on my own.

had to change "currentAmmo" to a Static variable. whilst altering my Health script to fit the need.

Script:

 #pragma strict
  
 var ammoPickup  : int = 10;
 var pickupSound : AudioClip;
 private var ammoScript : shootScript;//referencing "HealthValue" script externally
  
 function Awake () {
  
 var ammoScript = GetComponent(shootScript);//references external script called "HealthValue" with "fullHealth" variable
  
 }
  
 function Start () {
  
 }
  
 function Update () {
  
 }
  
 function OnTriggerEnter (col : Collider) {
  
 if(col.tag =="Player" && ammoScript.CurrentAmmo < 10) {
  
 ammoScript.CurrentAmmo += ammoPickup;
 ammoScript.canFire = true;//adds our healthPickup value to our total health
 AudioSource.PlayClipAtPoint(pickupSound, transform.position);//plays audio clip at the position of our cube
 Destroy(gameObject);
 }
  
 }
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 ownerfate · May 30, 2016 at 07:31 PM 0
Share

$$anonymous$$AJOR UPDATE

i reworked my ammo and shooting script to now function via 2 scripts. ( 1 for ammo, 1 for weapons )

i call it my newAgeGlobalShoot.js & newAgeGlobalReload.js they use static vars. to link each other, with an ID opposed to making tons of scripts.

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

15 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

Related Questions

Why did my boolean don't work? 0 Answers

Player will not stop shooting when ammo count = 0 1 Answer

Enemy character disappears as soon as i hit play 0 Answers

Raycast Shooting [Need help] 0 Answers

Enemy AI for Shooting Game 1 Answer


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