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 nedepoop · Jul 11, 2020 at 07:00 AM · gun script

Why does my full auto gun script not work?

Hello, everyone. I'm pretty new to unity and i made a simple target shooter game. I wanted to make it possible to switch between full auto (continuous firing if trigger is held) and semi auto (one shot per trigger pull) with the g key on my keyboard. My script has no errors, however it does not seem to switch to full auto. Here is The script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 public class Aim : MonoBehaviour {
 
     public GameObject ak47;
     public ParticleSystem mf;
     public float damage = 10f;
     public float range = 100f;
     public AudioSource audioSource;
     public Camera fpsCam;
     public bool fullAuto = false;
 
     void Start()
     {
     
     }
         // Update is called once per frame
     void Update()
     {
         if(Input.GetKeyDown(KeyCode.G) && fullAuto == false)
         {
             fullAuto = true;
         }
         if (Input.GetKeyDown(KeyCode.G) && fullAuto == true)
         {
             fullAuto = false;
 
         }
         bool leftClick = Input.GetMouseButtonDown(0);
 
         if (fullAuto == false && leftClick == true)
         {
             Shoot();
         }
         if (fullAuto == true && leftClick == true)
         {
             ShootFullAuto();
         }
         if (fullAuto == true)
         {
             Debug.Log("Full Auto");
         }
         if (fullAuto == false)
         {
             Debug.Log("Semi Auto");
         }
     }
     void Shoot()
     {
         audioSource.PlayOneShot(audioSource.clip);
         mf.Play();
         RaycastHit hit;
         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
         {
             
             Debug.Log(hit.transform.name);
             Target target = hit.transform.GetComponent<Target>();
             if (target != null)
             {
                 target.TakeDamage(damage);
             }
         }
     }
 
     IEnumerator ShootFullAuto()
     {
         while(true)
         {
             Shoot();
             yield return new WaitForSeconds(0.1F);
         }
     }
 
 }
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

4 Replies

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

Answer by nedepoop · Jul 11, 2020 at 09:57 AM

Hello Again! I think i might have found the solution to my problem. There is a great tutorial made by brackeys on youtube that i used to set up the raycasts in my script. It apparently also includes how to set up a fire rate and everything! Here is the link: https://www.youtube.com/watch?v=THnivyG0Mvo

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 olivermgbruce2002 · Jul 11, 2020 at 07:46 AM

What i would do is when your testing to shoot (if auto is on or not) on line 34 - line 41, try this instead.

 if (fullAuto == false){
     if (Input.GetButtonDown("Fire1")){
         Shoot();
     }
 } else {
    if (Input.GetButton("Fire1")){
         Shoot();
     }
 }

And you can delete the IEnumerator ShootFullAuto Statement. Fire1 is the left click of a mouse, and the Input.GetButtonDown is non-auto, while Input.GetButton is auto. The other thing to do is to take out the bool leftClick statement on line 32. Hope that helps, and feel free to ask me for more help or to understand what I've given you.

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 nedepoop · Jul 11, 2020 at 09:42 AM

Hello and thanks for replying! I should probably point out that the Debug.Log (Line 44 and 48) shows fullAuto Constantly set at false. Also, unfortunately your script didn't work for me.

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 FElineRAptor7273 · Sep 22, 2021 at 06:28 AM

If you remove IEnumerator, you cannot use yield return new WaitForSeconds. And If you are using IEnumerator then call the method ShootFullAuto by writing StartCoroutine(ShootFullAuto()); instead of writing ShootFullAuto(); And also write Input.GetButton("Fire1"); instead of Input.GetMouseButtonDown(0); just for full auto (not for semi auto as for that you might want to use Input.GetButtonDown("Fire1");)

Hope this helps :) And anyways if it doesn't work, you can then check out Brakeys video on shooting with raycasts.

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

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

Trying to make a alternating fire script for twin gun starfighter ( JAVA ). But both still fail to alternate and they fire at the same time. 1 Answer

How do i make my gun do damage? 1 Answer

A problem with GUN FIRE SOUND 1 Answer

Gun won't face target 0 Answers

How do i get my bullets to do damage?,i have a player than can shoot bullets but i don't know how to make the bullets do damage and go after. Can anyone help? 0 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