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);
}
}
}
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
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.
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.
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.
Your answer
Follow this Question
Related Questions
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