- Home /
Help with a C# Gun script
HI! :D 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 :D - Nikolai
Why don't you just use your working code? If you only want to convert it, then you can use this converter on the web.
Answer by HuskyPanda213 · Jan 25, 2014 at 10:48 PM
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. :)
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
}
Wait 1 more thing please :D How would I make the audio repeat? :3
It should repeat, because it is based upon the fire method, which does not change. It's only that it is called every frame the click is held down in auto and the frame that the mouse is clicked in semi.
For what it's worth, I would do something like this:
public enum Fire$$anonymous$$ode
{
Safe,
Single,
Semi,
Auto,
}
public Fire$$anonymous$$ode fire$$anonymous$$ode = Auto;
public int roundsFiredCounter = 0;
public int burstFireCount = 3;
private $$anonymous$$agazine magazine;
void Update()
{
if (!magazine.isEmpty)
{
switch(fire$$anonymous$$ode)
{
case Safe:
// don't do anything
break;
case Single:
if (GetButtonDown("Fire1"))
Fire();
break;
case Semi:
if (roundsFiredCounter++ < burstFireCount && GetButton("Fire1"))
Fire();
break;
case Auto:
if (GetButton("Fire1"))
Fire();
break;
}
}
// reset the rounds fired counter
if (GetButtonUp("Fire1"))
roundsFiredCounter = 0;
}
In that way you can have multiple fire modes that only work when there is ammo in the currently inserted magazine.
Answer by black1ops22 · Mar 17, 2015 at 10:52 PM
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613131434im_/https://answers.unity.com/themes/thub/images/avi.jpg)