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 $$anonymous$$ · Apr 24, 2019 at 07:11 PM · scripting problemshootingscriptingbasicsgun scriptgunfire

How would I add force/ burst fire to my gun?

Alright I know how to make Semi Auto and full auto guns but, burst fire is really confusing me. If any can help please comment below. And the Force is really confusing me to. So once again please comment bellow if you can help here is the script.

 using UnityEngine;
 using System.Collections;
 
 
 public class GunNo : MonoBehaviour
 {
     private const int V = 3;
 
     //Decal
     public GameObject decalHit;
   public float projectileDestroyTimer = 5f;
 
 
 
 
   //Ámmo
   public int maxAmmoTotal = 30; // Max ammo to carry Example: 30
   public int maxAmmoInClipTotal = 5; // Max ammo total to be able to reload Example: 5
   public int curAmmo = 30; // Current reloadable ammo Example: 30
   public int ammoInClip = 8; // Current ammo in clip Example: 5
   private bool isReloading = false;
 
 
 
 
   //Animation
   public string fireAnimation; //Shoot animation
   public string clipEmptyAnim; //Clip empty animation
   public string reloadAnimation; //Reload animation
   public float reloadTime = 1f; // How long it takes to reload
   public Animator reloadAnimations;
   public Animator ClipEmptyAnimations;
 
 
 
 
   //GameObjects
   public GameObject weapon; //Needed for aiming
   public ParticleSystem muzzleflash;
   public Animator FireAnimation;
   public AudioSource GunSounds;
 
 
 
 
   //Raycast
   private RaycastHit rayHit;
   public float shootDistance = 200f;
 
 
 
 
   //Recoil
   private float defaultRecoil = 50f;
   public float recoil = 50f;
   public float recoilAiming = 10f;
 
 
   //Aim
   public Vector3 aimPos = Vector3.zero;
   private Vector3 defaultPos = Vector3.zero;
   public float smoothTime; //How long it takes to get to the aiming position
   private Vector3 dampVelocity;
   private bool aiming = false;
 
 
   //Audio
   private AudioSource audioSource; //Audio source
   public AudioClip fireSound; //Sound when shooting
   public AudioSource clipEmptySound; //Sound when clip is empty
   public AudioSource reloadSound; //Sound when reloading
 
   //Weapon
   public int hitDamage = 13;
   float LastTimeShot;
   public float TimeBetweenShots = 1;
   public float fireRate = 20f;
 
   private float nextTimeToFire = 0f;
 
 
   //Only for testing
   int removedAmmoRecently; //Only for testing
 
 
   void Start()
   {
     defaultPos = weapon.transform.localPosition;
     defaultRecoil = recoil;
     audioSource = GetComponent<AudioSource>();
   }
 
 
   void Update()
   {
     AimDownSight();
 
      if (Input.GetButton("Fire1") && Time.time >=nextTimeToFire)
         {
             nextTimeToFire = Time.time + 1f / fireRate;
             Shoot();
       
     }
     if(Input.GetButtonDown("Fire2") && !isReloading)
     {
       aiming = !aiming;
     }
 
 
     if (Input.GetKeyDown(KeyCode.R) && !isReloading)
     {
       StartCoroutine("Reload");
     }
   }
 
 
   void Shoot()
   {
     if(ammoInClip != 0)
     {
       PlaySound(fireSound);
       GunSounds.Play();
       muzzleflash.Play();
       PlayAnimation(fireAnimation);
       GetComponent<Animator>().Play("FireAnimation");
       ammoInClip--;
       Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2f + Random.Range(-recoil, recoil), Screen.height/2f + Random.Range(-recoil, recoil), 0f));
       if (Physics.Raycast(ray, out rayHit, shootDistance))
       {
         GameObject decalClone = (GameObject)Instantiate(decalHit, rayHit.point, Quaternion.LookRotation(rayHit.normal, Vector3.forward));
         Destroy(decalClone, projectileDestroyTimer);
 
         DoDamage(rayHit);
       }
     }
     else{
       clipEmptySound.Play();
       GetComponent<Animator>().Play("ClipEmptyAnimation");
     }
     Debug.Log("Ammo: " + ammoInClip + "/" + curAmmo);
   }
 
 
   void DoDamage(RaycastHit hit)
   {
     if(hit.transform.tag == "Enemy")
     {
       if(hit.transform.GetComponent<EnemyHealth>()) //You can change EnemyHealth, to the name of the script that contains the health of the enemy
       {
         hit.transform.SendMessage("ApplyDamage", hitDamage, SendMessageOptions.DontRequireReceiver);
       }
       Debug.Log(hit.transform.name + " has been hit");
     }
   }
 
 
   IEnumerator Reload()
   {
     //If ammoInClip example 5 is equal or greater than  0 and ammoInClip(5) is lower than maxAmmoInClipTotal(The max amount of ammo that can be in a clip) and curAmmo(how much ammo you have like 5/30) is greater than 0
     if (ammoInClip >= 0 && ammoInClip < maxAmmoInClipTotal && curAmmo > 0)
     {
       isReloading = true;
       reloadSound.Play();
       GetComponent<Animator>().Play("reloadAnimations");
       yield return new WaitForSeconds(reloadTime);
       removedAmmoRecently = 0; //You can remove this line, it's only for testing.
       for (int i = 0; i < maxAmmoInClipTotal; i++)
       {
         if (ammoInClip == maxAmmoInClipTotal || curAmmo <= 0)
         {
           break;
         }
         else
         {
           ammoInClip++;
           curAmmo--;
           removedAmmoRecently++; //You can remove this line, it's only for testing. | To check how much ammo has been removed
         }
       }
       isReloading = false;
       Debug.Log("Removed " + removedAmmoRecently + " " + "ammo from pocket ammo. Ammo: " + ammoInClip + "/" + curAmmo); //You can remove this line, it's only for testing. | Prints out how much ammo that has been removed, ammo you have in clip, and how much ammo that is left
     }
     else
     {
       yield break;
     }
   }
 
 
   void PlayAnimation(string anim)
   {
     if (anim != "")
     {
       weapon.GetComponent<Animation>().Play(anim);
     }
   }
 
 
   void PlaySound(AudioClip clip)
   {
     if (clip != null)
     {
       audioSource.PlayOneShot(clip);
     }
   }
 
 
   void AimDownSight()
   {
     if(aiming && weapon.transform.localPosition.normalized != aimPos.normalized)
     {
       weapon.transform.localPosition = Vector3.SmoothDamp (weapon.transform.localPosition, aimPos, ref dampVelocity, smoothTime);
       Debug.Log ("Aiming");
       recoil = recoilAiming;
     }
 
 
     if(!aiming && weapon.transform.localPosition.normalized != defaultPos.normalized)
     {
       weapon.transform.localPosition = Vector3.SmoothDamp (weapon.transform.localPosition, defaultPos, ref dampVelocity, smoothTime);
       Debug.Log ("UnAiming");
       recoil = defaultRecoil;
     }
   }
 
 
   void OnGUI()
   {
     GUILayout.Label("Ammo: " + ammoInClip + "/" + curAmmo);
   }
 }
   
Comment
Add comment · Show 2
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 MorganSkillicorn · Apr 25, 2019 at 12:29 PM 0
Share

can you try and fix your code tags :)

avatar image $$anonymous$$ MorganSkillicorn · Apr 25, 2019 at 02:29 PM 0
Share

Hold on...

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by edthered1009 · Apr 25, 2019 at 11:38 PM

Not to be rude, but I don't enitrely get what makes it so hard. Couldn't you just make a method that runs the Shoot() method three times? Obviously, you'd have to add some form of timer in between shots, or else it'd be a shotgun, but it wouldn't be very hard if you put your mind to it.

Hope this helps!

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 $$anonymous$$ · Apr 26, 2019 at 12:09 AM 0
Share

Thanks I Solved it by using exactly what you just said.

avatar image
0

Answer by $$anonymous$$ · Apr 25, 2019 at 02:33 PM

 using UnityEngine;
 using System.Collections;
 
 
 public class GunNo : MonoBehaviour
 {
     private const int V = 3;
 
     //Decal
     public GameObject decalHit;
   public float projectileDestroyTimer = 5f;
 
 
 
 
   //Ámmo
   public int maxAmmoTotal = 30; // Max ammo to carry Example: 30
   public int maxAmmoInClipTotal = 5; // Max ammo total to be able to reload Example: 5
   public int curAmmo = 30; // Current reloadable ammo Example: 30
   public int ammoInClip = 8; // Current ammo in clip Example: 5
   private bool isReloading = false;
 
 
 
 
   //Animation
   public string fireAnimation; //Shoot animation
   public string clipEmptyAnim; //Clip empty animation
   public string reloadAnimation; //Reload animation
   public float reloadTime = 1f; // How long it takes to reload
   public Animator reloadAnimations;
   public Animator ClipEmptyAnimations;
 
 
 
 
   //GameObjects
   public GameObject weapon; //Needed for aiming
   public ParticleSystem muzzleflash;
   public Animator FireAnimation;
   public AudioSource GunSounds;
 
 
 
 
   //Raycast
   private RaycastHit rayHit;
   public float shootDistance = 200f;
 
 
 
 
   //Recoil
   private float defaultRecoil = 50f;
   public float recoil = 50f;
   public float recoilAiming = 10f;
 
 
   //Aim
   public Vector3 aimPos = Vector3.zero;
   private Vector3 defaultPos = Vector3.zero;
   public float smoothTime; //How long it takes to get to the aiming position
   private Vector3 dampVelocity;
   private bool aiming = false;
 
 
   //Audio
   private AudioSource audioSource; //Audio source
   public AudioClip fireSound; //Sound when shooting
   public AudioSource clipEmptySound; //Sound when clip is empty
   public AudioSource reloadSound; //Sound when reloading
 
   //Weapon
   public int hitDamage = 13;
   float LastTimeShot;
   public float TimeBetweenShots = 1;
   public float fireRate = 20f;
 
   private float nextTimeToFire = 0f;
 
 
   //Only for testing
   int removedAmmoRecently; //Only for testing
 
 
   void Start()
   {
     defaultPos = weapon.transform.localPosition;
     defaultRecoil = recoil;
     audioSource = GetComponent<AudioSource>();
   }
 
 
   void Update()
   {
     AimDownSight();
 
      if (Input.GetButton("Fire1") && Time.time >=nextTimeToFire)
         {
             nextTimeToFire = Time.time + 1f / fireRate;
             Shoot();
       
     }
     if(Input.GetButtonDown("Fire2") && !isReloading)
     {
       aiming = !aiming;
     }
 
 
     if (Input.GetKeyDown(KeyCode.R) && !isReloading)
     {
       StartCoroutine("Reload");
     }
   }
 
 
   void Shoot()
   {
     if(ammoInClip != 0)
     {
       PlaySound(fireSound);
       GunSounds.Play();
       muzzleflash.Play();
       PlayAnimation(fireAnimation);
       GetComponent<Animator>().Play("FireAnimation");
       ammoInClip--;
       Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2f + Random.Range(-recoil, recoil), Screen.height/2f + Random.Range(-recoil, recoil), 0f));
       if (Physics.Raycast(ray, out rayHit, shootDistance))
       {
         GameObject decalClone = (GameObject)Instantiate(decalHit, rayHit.point, Quaternion.LookRotation(rayHit.normal, Vector3.forward));
         Destroy(decalClone, projectileDestroyTimer);
 
         DoDamage(rayHit);
       }
     }
     else{
       clipEmptySound.Play();
       GetComponent<Animator>().Play("ClipEmptyAnimation");
     }
     Debug.Log("Ammo: " + ammoInClip + "/" + curAmmo);
   }
 
 
   void DoDamage(RaycastHit hit)
   {
     if(hit.transform.tag == "Enemy")
     {
       if(hit.transform.GetComponent<EnemyHealth>()) //You can change EnemyHealth, to the name of the script that contains the health of the enemy
       {
         hit.transform.SendMessage("ApplyDamage", hitDamage, SendMessageOptions.DontRequireReceiver);
       }
       Debug.Log(hit.transform.name + " has been hit");
     }
   }
 
 
   IEnumerator Reload()
   {
     //If ammoInClip example 5 is equal or greater than  0 and ammoInClip(5) is lower than maxAmmoInClipTotal(The max amount of ammo that can be in a clip) and curAmmo(how much ammo you have like 5/30) is greater than 0
     if (ammoInClip >= 0 && ammoInClip < maxAmmoInClipTotal && curAmmo > 0)
     {
       isReloading = true;
       reloadSound.Play();
       GetComponent<Animator>().Play("reloadAnimations");
       yield return new WaitForSeconds(reloadTime);
       removedAmmoRecently = 0; //You can remove this line, it's only for testing.
       for (int i = 0; i < maxAmmoInClipTotal; i++)
       {
         if (ammoInClip == maxAmmoInClipTotal || curAmmo <= 0)
         {
           break;
         }
         else
         {
           ammoInClip++;
           curAmmo--;
           removedAmmoRecently++; //You can remove this line, it's only for testing. | To check how much ammo has been removed
         }
       }
       isReloading = false;
       Debug.Log("Removed " + removedAmmoRecently + " " + "ammo from pocket ammo. Ammo: " + ammoInClip + "/" + curAmmo); //You can remove this line, it's only for testing. | Prints out how much ammo that has been removed, ammo you have in clip, and how much ammo that is left
     }
     else
     {
       yield break;
     }
   }
 
 
   void PlayAnimation(string anim)
   {
     if (anim != "")
     {
       weapon.GetComponent<Animation>().Play(anim);
     }
   }
 
 
   void PlaySound(AudioClip clip)
   {
     if (clip != null)
     {
       audioSource.PlayOneShot(clip);
     }
   }
 
 
   void AimDownSight()
   {
     if(aiming && weapon.transform.localPosition.normalized != aimPos.normalized)
     {
       weapon.transform.localPosition = Vector3.SmoothDamp (weapon.transform.localPosition, aimPos, ref dampVelocity, smoothTime);
       Debug.Log ("Aiming");
       recoil = recoilAiming;
     }
 
 
     if(!aiming && weapon.transform.localPosition.normalized != defaultPos.normalized)
     {
       weapon.transform.localPosition = Vector3.SmoothDamp (weapon.transform.localPosition, defaultPos, ref dampVelocity, smoothTime);
       Debug.Log ("UnAiming");
       recoil = defaultRecoil;
     }
   }
 
 
   void OnGUI()
   {
     GUILayout.Label("Ammo: " + ammoInClip + "/" + curAmmo);
   }
 }
   
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 $$anonymous$$ · Apr 25, 2019 at 02:34 PM 0
Share

@$$anonymous$$organSkillicorn There you go.

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

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

How can I shoot bullets that you can aim with the crosshair? 1 Answer

Having trouble using SendMessage 1 Answer

How would i make it transform player.position =target.position? 2 Answers

UNet Shooting Projectiles easy method? 0 Answers

FPS recoil 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