• 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 EliteHedgehog56 · Aug 20, 2017 at 01:55 AM · shootingammopickups

limited ammo and ammo pickups

In my splitscreen shooter instead of having to pick up weapons, each plaer has all the weaons at the start but so far all the guns have unlimited ammo, I am using an automatic rigidbody shooting script that is added to an empty prefab child in each weapon prefab called 'firing point', I want to change my shooting script to have a limited supply of held ammo. So basically when a weapon's ammo has depleated I want it to disable the firing point child so the player cant shoot it anymore and then when the player comes into contact with an ammo pickup I want to tell the ammo pickup to send a message to the shooting script to add ammo to the weapon and re-enable the firing point child so the player can use the weapon again.

here's my script

RequireComponent(AudioSource); var audioSource: AudioSource; var Projectile : Rigidbody; var ProjectileSpeed : int = 10; var FireRate : float = 10; // The number of bullets fired per second var lastfired : float; // The value of Time.time at the last firing moment var gunshot : AudioClip; var muzzleflash :Transform;

function Start() { audioSource = GetComponent.(); }

function Update () { if (Input.GetAxis("Fire1")>0f) { if (Time.time - lastfired > 1 / FireRate) { lastfired = Time.time; var clone : Rigidbody; clone = Instantiate(Projectile, transform.position, transform.rotation); clone.velocity = transform.TransformDirection (Vector3.forward * ProjectileSpeed); audioSource.PlayOneShot(gunshot, 0.7F); Instantiate (muzzleflash,transform.position,transform.rotation); } } }

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
0

Answer by AbominationGames · Aug 20, 2017 at 05:51 AM

You technically don't need to disable the firing point. You can create a boolean canShoot and set it to false or true. Then you can just toggle the boolean, this will also allow for you to add to your script later, (weapon jams, reloading, hit, object interference, etc).

PSEUDO CODE: ammo = 0; canFire = false;

Update: if ammo >= 0 canFire = true

if (canFire) { if(Input.GetAxis("Fire1") && Time.time - lastfired > 1 / FireRate) { Shoot();

Shoot() { InstantiateBuller(); ResetShotTimer(); }

So this way you never have to disable your fire point you just can't shoot whenever your bool is set to false. This allows you to make it to where if you're falling, or in the water later you can't shoot. It will allow you to expand later on this way much easier than having to deal with disabling the objects.

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 EliteHedgehog56 · Oct 08, 2017 at 08:47 AM 0
Share

@gamemonk

what I want to do is make it similar to turok rage wars on n64, where the player starts with some ammo for their first weapon (for my case the pistol)

that boolean looks like what I need but coudl you please add it into my script in my answer because I don't quite know where to put your code snippets

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

69 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Gun with limited ammo? 1 Answer

Projectile Firing/Gun script 1 Answer

reload script does not work when ammo is 16 2 Answers

adding ammo count 2 Answers

Stopping ammo to be able to pick up. 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