C# ShootController()

hey guys. I have a problem with my ShootController(). Specifically when I play the game and press (“Fire1”) for shooting my weapon shoots 3 bullets with one click. I cant imagine the problem So I decided to post it here.

Here is my script (I removed the parts which has nothing to do with Shooting):

//Sound variables

public GameObject bulletSound;

public GameObject holdSound;

public void Update()
    {
        ShootController();
    }

public void ShootController()
	{
		if (Input.GetButton("Fire1"))
        {
            if (shoottime <= Time.time)
            {
                shoottime = Time.time + CurrentWeapon.firerate;
                CurrentRecoil1 += new Vector3(CurrentWeapon.RecoilRotation.x, Random.Range(-CurrentWeapon.RecoilRotation.y, CurrentWeapon.RecoilRotation.y));
                CurrentRecoil3 += new Vector3(Random.Range(-CurrentWeapon.RecoilKickback.x, CurrentWeapon.RecoilKickback.x), Random.Range(-CurrentWeapon.RecoilKickback.y, CurrentWeapon.RecoilKickback.y), CurrentWeapon.RecoilKickback.z);
            }
			
			GameObject inst_bullet = Instantiate(CurrentWeapon.Bullet, CurrentWeapon.Spawnpoint.position, CurrentWeapon.Spawnpoint.rotation) as GameObject;
			inst_bullet.rigidbody.AddRelativeForce(Vector3.forward, ForceMode.Impulse);
			Physics.IgnoreCollision(inst_bullet.collider, CharCont);
        	inst_bullet.GetComponent<BulletManager>().damage = CurrentWeapon.weaponDamage;
			if (bulletSound)
			{
				holdSound = Instantiate(bulletSound, CurrentWeapon.Spawnpoint.transform.position, CurrentWeapon.Spawnpoint.transform.rotation) as GameObject;
			}
			if (holdSound)
			{
				holdSound.transform.parent = transform;
			}
		}
	}
public void RecoilController()
    {
        CurrentRecoil1 = Vector3.Lerp(CurrentRecoil1, Vector3.zero, 0.1f);
        CurrentRecoil2 = Vector3.Lerp(CurrentRecoil2, CurrentRecoil1, 0.1f);
        CurrentRecoil3 = Vector3.Lerp(CurrentRecoil3, Vector3.zero, 0.1f);
        CurrentRecoil4 = Vector3.Lerp(CurrentRecoil4, CurrentRecoil3, 0.1f);
 		RecoilHolder.localEulerAngles = CurrentRecoil2;
        RecoilHolder.localPosition = CurrentRecoil4;
    }
[System.Serializable]
public class WeaponInfo
{
    public string name = "Weapon";
    public float firerate = 0.1f;
	public float bulletSpeed = 1;
	public float weaponDamage = 10;
    public Transform WeaponTransform;
	public Transform Spawnpoint;
 	public Vector3 RecoilRotation;
    public Vector3 RecoilKickback;
	public GameObject Bullet;
 	public int CurrentScope;
    public List<WeaponScope> Scopes = new List<WeaponScope>();
}

I don’t know what the value of CurrentWeapon.fireRate is, but you would want to use a different input method:

if (Input.GetButtonDown("Fire1"))

GetButtonDown returns true only if the button was just pressed. The one you used is GetButton which returns true every frame if the player holds the button.

Here’s some pseudocode of how I did a machinegun in a recent prototype (m_fireRate = 0.1f):

if (Input.GetButton("Fire1"))
{
    if (Time.time - m_lastFire > m_fireRate)
    {
        GameObject inst_bullet = Instantiate(CurrentWeapon.Bullet, CurrentWeapon.Spawnpoint.position, CurrentWeapon.Spawnpoint.rotation) as GameObject;
        inst_bullet.rigidbody.AddRelativeForce(Vector3.forward, ForceMode.Impulse);
        Physics.IgnoreCollision(inst_bullet.collider, CharCont);
        inst_bullet.GetComponent<BulletManager>().damage = CurrentWeapon.weaponDamage;
        if (bulletSound)
        {
            holdSound = Instantiate(bulletSound, CurrentWeapon.Spawnpoint.transform.position, CurrentWeapon.Spawnpoint.transform.rotation) as GameObject;
        }
        if (holdSound)
        {
            holdSound.transform.parent = transform;
        }

        // Record fire time
        m_lastFire = Time.time;
    }
}

EDIT: Modified to include your bullet spawning code.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PlayerController : MonoBehaviour
{
public CharacterController CharCont;
public CharacterMotor CharMotor;
//Holder for Weapons
public Transform WalkAnimationHolder;
public Transform JumpAnimationHolder;
public Transform ADSHolder;
public Transform SwayHolder;
public Transform RecoilHolder;
public WalkingState walkingstate = WalkingState.Idle;
public float VelocityMagnitude;
public float WalkSpeed;
public float RunSpeed;
public bool WasStanding;

//Player Variables
public bool IsAiming;
public WeaponInfo CurrentWeapon;
public List<WeaponInfo> WeaponList = new List<WeaponInfo>();
public Vector3 CurrentRecoil1;
public Vector3 CurrentRecoil2;
public Vector3 CurrentRecoil3;
public Vector3 CurrentRecoil4;
private float shoottime = 0;

//Sounds
public GameObject bulletSound;
public GameObject holdSound;

void Start()
{
    CurrentWeapon = WeaponList[0];
}

public void Update()
{
    ShootController();
}
   
public void FixedUpdate()
{
    AnimationController();
    SwayController();
    SpeedController();
    RecoilController();
    ADSController();
    VelocityMagnitude = CharCont.velocity.magnitude;
}

public void SpeedController()
{
    if ((Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0) && VelocityMagnitude > 0)
    {
        if (Input.GetButton("Run"))
        {
            walkingstate = WalkingState.Running;
            CharMotor.movement.maxForwardSpeed = RunSpeed;
            CharMotor.movement.maxSidewaysSpeed = RunSpeed;
            CharMotor.movement.maxBackwardsSpeed = RunSpeed / 2;
        }
        else
        {
            walkingstate = WalkingState.Walking;
            CharMotor.movement.maxForwardSpeed = WalkSpeed;
            CharMotor.movement.maxSidewaysSpeed = WalkSpeed;
            CharMotor.movement.maxBackwardsSpeed = WalkSpeed / 2;
        }
    }
    else
    {
        walkingstate = WalkingState.Idle;
    }
}
   
public void AnimationController()
{
    if(WasStanding && !CharCont.isGrounded)
	{
		WasStanding = false;
		WalkAnimationHolder.animation.Play("animationSTANDINGJUMP");
	}
	else if (!WasStanding && CharCont.isGrounded)
	{
		WasStanding = true;
		WalkAnimationHolder.animation.Play("animationSTANDINGJUMPLANDING");
	}
	if(walkingstate == WalkingState.Running)
	{
		WalkAnimationHolder.animation["animationRUN"].speed = VelocityMagnitude / RunSpeed * 1.2f;
		WalkAnimationHolder.animation.CrossFade("animationRUN", 0.2f);
	}
	else if (walkingstate == WalkingState.Walking)
	{
		WalkAnimationHolder.animation["animationWALK"].speed = VelocityMagnitude / WalkSpeed * 1.2f;
		WalkAnimationHolder.animation.CrossFade("animationWALK", 0.2f);
	}
	else
	{
		WalkAnimationHolder.animation.CrossFade("animationIDLE", 0.2f);
	}
 }
   
public void SwayController()
    {
       
    }

public void ShootController()
{
	if (Input.GetButton("Fire1") && walkingstate != WalkingState.Running)
    {
        if (shoottime <= Time.time)
        {
            shoottime = Time.time + CurrentWeapon.firerate;
            CurrentRecoil1 += new Vector3(CurrentWeapon.RecoilRotation.x, Random.Range(-CurrentWeapon.RecoilRotation.y, CurrentWeapon.RecoilRotation.y));
            CurrentRecoil3 += new Vector3(Random.Range(-CurrentWeapon.RecoilKickback.x, CurrentWeapon.RecoilKickback.x), Random.Range(-CurrentWeapon.RecoilKickback.y, CurrentWeapon.RecoilKickback.y), CurrentWeapon.RecoilKickback.z);
        }
		
		GameObject inst_bullet = Instantiate(CurrentWeapon.Bullet, CurrentWeapon.Spawnpoint.position, CurrentWeapon.Spawnpoint.rotation) as GameObject;
		inst_bullet.rigidbody.AddRelativeForce(Vector3.forward, ForceMode.Impulse);
		Physics.IgnoreCollision(inst_bullet.collider, CharCont);
    	inst_bullet.GetComponent<BulletManager>().damage = CurrentWeapon.weaponDamage;
		if (bulletSound)
		{
			holdSound = Instantiate(bulletSound, CurrentWeapon.Spawnpoint.transform.position, CurrentWeapon.Spawnpoint.transform.rotation) as GameObject;
		}
		if (holdSound)
		{
			holdSound.transform.parent = transform;
		}
	}
}

public void RecoilController()
{
    CurrentRecoil1 = Vector3.Lerp(CurrentRecoil1, Vector3.zero, 0.1f);
    CurrentRecoil2 = Vector3.Lerp(CurrentRecoil2, CurrentRecoil1, 0.1f);
    CurrentRecoil3 = Vector3.Lerp(CurrentRecoil3, Vector3.zero, 0.1f);
    CurrentRecoil4 = Vector3.Lerp(CurrentRecoil4, CurrentRecoil3, 0.1f);
	RecoilHolder.localEulerAngles = CurrentRecoil2;
    RecoilHolder.localPosition = CurrentRecoil4;
}

public void ADSController()
{
    if (Input.GetButton("Fire2"))
    {
        IsAiming = true;
		ADSHolder.localPosition = Vector3.Lerp(ADSHolder.localPosition, CurrentWeapon.Scopes[CurrentWeapon.CurrentScope = 0].adsposition, 0.25f);
    }
    else
    {
        IsAiming = false;
        ADSHolder.localPosition = Vector3.Lerp(ADSHolder.localPosition, Vector3.zero, 0.25f);
    }
}

}

public enum WalkingState
{
Idle,
Walking,
Running,
}

[System.Serializable]
public class WeaponInfo
{
public string name = “Weapon”;
public float firerate = 0.1f;
public float bulletSpeed = 1;
public float weaponDamage = 10;
public Transform WeaponTransform;
public Transform Spawnpoint;
public Vector3 RecoilRotation;
public Vector3 RecoilKickback;
public GameObject Bullet;
public int CurrentScope;
public List Scopes = new List();
}

[System.Serializable]
public class WeaponScope
{
public string name;
public float fov;
public Vector3 adsposition;
public Transform scopetransform;
}