HI!
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
- Nikolai