Game Development Reference
In-Depth Information
020 //------------------------------------------------
021 // Use this for initialization
022 void Start ()
023 {
024 //Find sound object in scene
025 GameObject SoundsObject = GameObject.FindGameObjectWithTag("sounds");
026
027 //If no sound object, then exit
028 if(SoundsObject == null) return;
029
030 //Get audio source component for sfx
031 SFX = SoundsObject.GetComponent<AudioSource>();
032
033 //Get all child sprite renderers for weapon
034 WeaponSprites = gameObject.GetComponentsInChildren<SpriteRenderer>();
035
036 //Register weapon for weapon change events
037 GameManager.Notifications.AddListener(this, "WeaponChange");
038 }
039 //------------------------------------------------
040 // Update is called once per frame
041 void Update ()
042 {
043 //If not equipped then exit
044 if(!IsEquipped) return;
045
046 //If cannot accept input, then exit
047 if(!GameManager.Instance.InputAllowed) return;
048
049 //Check for fire button input
050 if(Input.GetButton("Fire1") && CanFire)
051 StartCoroutine(Fire());
052 }
053 //------------------------------------------------
054 //Coroutine to fire weapon
055 public IEnumerator Fire()
056 {
057 //If can fire
058 if(!CanFire || !IsEquipped || Ammo <= 0) yield break;
059
060 //Set refire to false
061 CanFire = false;
062
063 //Play Fire Animation
064 gameObject.SendMessage("PlaySpriteAnimation", 0,
SendMessageOptions.DontRequireReceiver);
065
066 //Play collection sound, if audio source is available
067 if(SFX){SFX.PlayOneShot(WeaponAudio, 1.0f);}
068
Search WWH ::




Custom Search