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 /
  • Help Room /
avatar image
0
Question by CrizzieBabe · Feb 17, 2017 at 05:13 PM · shootingbulletscollectible

I have a script that fires bullets and applies damage just the way I want to, but I am struggling to collect ammo and count it and run out of ammo. This is my script of what I have working so far. Please help.

using UnityEngine; using System.Collections;

public class RaycastShoot : MonoBehaviour {

 public int gunDamage = 1;
 public float fireRate = .25f;
 public float weaponRange = 50f;
 public float hitForce = 100f;
 public Transform gunEnd;

 private Camera fpsCam;
 private WaitForSeconds shotDuration = new WaitForSeconds(.07f);
 private AudioSource gunAudio;
 private LineRenderer laserLine;
 private float nextFire;

 void Start ()
 {
     laserLine = GetComponent<LineRenderer>();
     gunAudio = GetComponent<AudioSource>();
     fpsCam = GetComponentInParent<Camera>();
 }
 

 void Update ()
 {
     if (Input.GetButtonDown ("Fire1") && Time.time > nextFire)
     {
         nextFire = Time.time + fireRate;

         StartCoroutine(ShotEffect());

         Vector3 rayOrigin = fpsCam.ViewportToWorldPoint (new Vector3(0.5f, 0.5f, 0));
         RaycastHit hit;

         laserLine.SetPosition(0, gunEnd.position);

         if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
         {
             laserLine.SetPosition(1, hit.point);

             EnemyHealth health = hit.collider.GetComponent<EnemyHealth>();

             if (health != null)
             {
                 health.TakeDamage(gunDamage);
             }

             if (hit.rigidbody != null)
             {
                 hit.rigidbody.AddForce(-hit.normal * hitForce);
             }
         }
         else
         {
             laserLine.SetPosition (1, rayOrigin + (fpsCam.transform.forward * weaponRange));
         }
     }
 }

 private IEnumerator ShotEffect ()
 {
     gunAudio.Play();

     laserLine.enabled = true;
     yield return shotDuration;
     laserLine.enabled = false;
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Pharez · Feb 17, 2017 at 06:04 PM

You just have a numerical value as a variable and make it so that the weapon only fires if it is greater than 0. Than decrement that value whenever the weapon is fired.

e.g:

int ammo = 3;
if (Input.GetButtonDown("Fire1") && Time.Time < nextFire && ammo>0)
{
ammo--;
//*Your weapon firing stuff here*
}

Then to do ammo pickups you

  • Make another game object

  • Put a collider on that object

  • Check the box that says "Is Trigger"

  • Put a new script on that object call it something like "AmmoPickUp"

  • Make a public method in your RayCastShoot Class called AmmoPickUp

  • The function should take in a value called 'amount' and just add it to the ammo variable in that class

  • Now go back to your AmmoPickUp class and do the following :

{ [SerializeField] int amountPickedUp = 20;

 void OnTriggerEnter(collider other)
 {
    RayCastShoot rcs =  GetComponent<RayCastShoot>();
 
    if(rcs != null)
    {        
       rcs.AmmoPickUp(amountPickedUp);
    }
 
 }
 

[SerializeField] Makes it so that a variable is shown in the editor window without having to make it public

Stuff about OnTriggerEnter is available from https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html

Also, you may want to look at using tags instead of checking if a variable after using a GetComponent is null as I believe trying to get a component that doesn't exist will throw an error anyway. In which case it would have been probably something like:

if (other.tag = "Player")
  rcs.AmmoPickUp(AmountPickedUp);

Hope this helps

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 CrizzieBabe · Feb 27, 2017 at 03:58 PM

@Pharez Thank you very much, I have gotten most of it working, but I'm sorry I am a little confused with these two instructions:

  • Make a public method in your RayCastShoot Class called AmmoPickUp

    The function should take in a value called 'amount' and just add it to the ammo variable in that class

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

94 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 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

Shoot at mouse cursor 0 Answers

2D topdown projectiles movement 0 Answers

Problem with bullets following camera 0 Answers

Help Spawn Bullet 2D 0 Answers

Isometric shooter - Bullets 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