Help with a C# Gun script

HI! :smiley:
I am following a Unity multiplayer tutorial and I just finished his gun tutorial. It fires semi-automatic(it only fires once when you pull the trigger), but I want it to be full-auto. I’ve programmed a full-auto script in Javascript, but never c#. This is my code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Gun : MonoBehaviour {
	public float FireRate;
	private float nextFireTime; 
	public float MinDamage;
	public float MaxDamage;
	private float ActualDamage;
	private float FireTime;
	public float Range = 800;
	public Transform SpawnPoint;
	public string Name;
	public GameObject ImpactEffect;
	public Transform AimObj;
	public List<Sight> Sights = new List<Sight>();
	public int CurSight;
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if(Input.GetButtonDown("Fire1"))
			Fire();
		
		
		
		foreach(Sight s in  Sights)
		{
			if(Sights.IndexOf(s) == CurSight)
			{
				s.Obj.SetActive(true);
			}
			else
			{
				s.Obj.SetActive(false);
			}
		}
	}
	
	public void Fire()
	{
		if(FireTime <= Time.time)
		{
			FireTime = FireRate + Time.time;
			ActualDamage = Random.Range(MinDamage,MaxDamage);
			audio.Play();
			NetworkManager.Instance.MyPlayer.Manager.Client_DoSound(Name);
			RaycastHit hit;
			if(Physics.Raycast(SpawnPoint.position,SpawnPoint.forward,out hit,Range)){
				if(hit.collider.rigidbody)
				{
					hit.collider.rigidbody.AddForceAtPosition((ActualDamage * 5) * transform.forward, hit.collider.transform.position);
				}
				UserPlayer hitter = hit.transform.root.GetComponent<UserPlayer>();
				Network.Instantiate(ImpactEffect,hit.point,Quaternion.FromToRotation(hit.normal,Vector3.up),0);
				if(hitter != null && hitter.MyPlayer.Team != NetworkManager.Instance.MyPlayer.Team)
				{
					hitter.networkView.RPC("Server_TakeDamge", RPCMode.All, ActualDamage);
					hitter.networkView.RPC("FindHitter", RPCMode.All, NetworkManager.Instance.MyPlayer.PlayerName, Name);
				}
			}
		}
		
	}
	
	
	void FixedUpdate()
	{
		if(Input.GetButton("Fire2"))
		{
			AimObj.transform.localPosition = Vector3.Lerp(AimObj.transform.localPosition,Sights[CurSight].AimPos, 0.25f);
		}
		else
		{
			AimObj.transform.localPosition = Vector3.Lerp(AimObj.transform.localPosition,Vector3.zero,0.25f);
		}
	}
}

[System.Serializable]
public class Sight
{
	public string Name;
	public GameObject Obj;
	public Vector3 AimPos;
}

What would I add to this script and where? Would I add a delay timer or what? Any help is appreciated! Thanks in advance :smiley:

  • Nikolai

Ok I know a solution, use Input.GetButton(YourButton) instead of Input.GetButtonDown(YourButton). I will write some code, which will be auto and semi auto :). Just tweak in the inspector, also I know what tutorial your are following, if you decide to add ammo, make sure you reset when you respawn. :slight_smile:

using UnityEngine;
	using System.Collections; 
	using System.Collections.Generic;
	
	public class Gun : MonoBehaviour {
		public float FireRate;
		private float nextFireTime; 
		public float MinDamage;
		public float MaxDamage;
		private float ActualDamage;
		private float FireTime;
		public float Range = 800;
		public Transform SpawnPoint;
		public string Name;
		public GameObject ImpactEffect;
		public Transform AimObj;
		public List<Sight> Sights = new List<Sight>();
		public int CurSight;
		//Change in inspector for result.
		public FireMode MyFiremode = FireMode.Semi;
		
		// Use this for initialization
		void Start () {
			
		}
		
		// Update is called once per frame
		void Update ()
		{
			if (MyFiremode == FireMode.Semi) {
				if (Input.GetButtonDown ("Fire1")){
					Fire ();
				}
			}

			if (MyFiremode == FireMode.Auto) {
				if (Input.GetButton ("Fire1")){
					Fire ();
				}
			}
			
			
			foreach(Sight s in  Sights)
			{
				if(Sights.IndexOf(s) == CurSight)
				{
					s.Obj.SetActive(true);
				}
				else
				{
					s.Obj.SetActive(false);
				}
			}
		}
		
		public void Fire()
		{
			if(FireTime <= Time.time)
			{
				FireTime = FireRate + Time.time;
				ActualDamage = Random.Range(MinDamage,MaxDamage);
				audio.Play();
				NetworkManager.Instance.MyPlayer.Manager.Client_DoSound(Name);
				RaycastHit hit;
				if(Physics.Raycast(SpawnPoint.position,SpawnPoint.forward,out hit,Range)){
					if(hit.collider.rigidbody)
					{
						hit.collider.rigidbody.AddForceAtPosition((ActualDamage * 5) * transform.forward, hit.collider.transform.position);
					}
					UserPlayer hitter = hit.transform.root.GetComponent<UserPlayer>();
					Network.Instantiate(ImpactEffect,hit.point,Quaternion.FromToRotation(hit.normal,Vector3.up),0);
					if(hitter != null && hitter.MyPlayer.Team != NetworkManager.Instance.MyPlayer.Team)
					{
						hitter.networkView.RPC("Server_TakeDamge", RPCMode.All, ActualDamage);
						hitter.networkView.RPC("FindHitter", RPCMode.All, NetworkManager.Instance.MyPlayer.PlayerName, Name);
					}
				}
			}
			
		}
		
		
		void FixedUpdate()
		{
			if(Input.GetButton("Fire2"))
			{
				AimObj.transform.localPosition = Vector3.Lerp(AimObj.transform.localPosition,Sights[CurSight].AimPos, 0.25f);
			}
			else
			{
				AimObj.transform.localPosition = Vector3.Lerp(AimObj.transform.localPosition,Vector3.zero,0.25f);
			}
		}
	}
	
	[System.Serializable]
	public class Sight
	{
		public string Name;
		public GameObject Obj;
		public Vector3 AimPos;
	}
	public enum FireMode{
		Auto,
		Semi
	}

I would make a fully functional script…

using UnityEngine;
using System.Collections;
//Bulletspawnpoint MUST be named Bulletspawnpoint
public class Tryintoscriptpewpews : MonoBehaviour {

public Rigidbody Bullet; // Bullet, Bulletspawnpoint, Automatic, isShooting,Bulletsound, Magsize, Ammo, firerate, Blood, Range, Damage, Counter, Bullethole, Magsize, Ammo, Bulletsound, Bulletanim, Bulletholesmoke, Barrelflash
public GameObject Barrelflash;
public GameObject Bulletholesmoke;
public GameObject Bulletspawnpoint;
public GameObject Bullethole;
public GameObject Blood;
public bool Automatic;
public float Range = 100f;
public float Firerate = 8f;
public float Magsize = 30f;
public float Amountofclips = 4f;
public float Damage = 15f;
private float Ammo;
private float Counter = 0f; 
private bool isShooting;
public AudioClip Bulletsound;
public AudioClip Reloadsound;
private RaycastHit hit;
public Animation Reloadanim;

// These are the customizable values.
// Create a spawnpoint and place it infront of the gun with a tag of “Bulletspawnpoint”

void Update(){
	Counter += Time.deltaTime;
	if(Automatic == true) {
		if(Input.GetKey (KeyCode.Mouse0) && Firerate > Counter && Ammo > 0 && Amountofclips > -1){
			isShooting = true;
			if(isShooting == true){Fire();}
			}
		Counter = 0f;
		}
	else {
			if(Input.GetKeyDown (KeyCode.Mouse0) && Ammo > 0 && Firerate > Counter && Amountofclips > -1){
			isShooting = true;
			if(isShooting == true){Fire();}
		}
	} 
}

void Fire (){
	Instantiate (Barrelflash, GameObject.Find ("Bulletspawnpoint").transform.position, GameObject.Find ("Bulletspawnpoint").transform.rotation);
		Instantiate (Bullet, GameObject.Find ("Bulletspawnpoint").transform.position, GameObject.Find ("Bulletspawnpoint").transform.rotation);
		Bullet.velocity = Vector3.forward * 8;
		AudioSource.PlayClipAtPoint (Bulletsound, transform.position);
	    Ammo = Magsize;
	    Ammo --; 
	if (Ammo == 0 && Amountofclips > -1) {

	}
	if (Physics.Raycast (GameObject.Find ("Bulletspawnpoint").transform.position, transform.forward, Range)) {
		if(hit.collider.gameObject.tag == "Player"){
			Instantiate(Blood, hit.point, hit.transform.rotation);
			hit.collider.gameObject.SendMessage("ApplyDamage", Damage);
		}
		else {
			Instantiate(Bullethole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
			Instantiate(Bulletholesmoke, hit.point, Quaternion.identity);
		}
	}
}

void Reload(){
	GetComponent<Animation>().Play ("Reloadanim", PlayMode.StopAll);
		AudioSource.PlayClipAtPoint(Reloadsound, transform.position);

}

}

just remember to reset the ammo when u respawn, OR (easier) in a health script destroy the object and in a game/network manager after X seconds intantiate the player