Game Development Reference
In-Depth Information
042 //If cannot accept input, then exit
043 if(!GameManager.Instance.InputAllowed) return;
044
045 //Check for fire button input
046
if(Input.GetButton("Fire1")
&&
CanFire)
047 StartCoroutine(Fire());
048 }
049 //------------------------------------------------
050 //Coroutine to fire weapon
051 public IEnumerator Fire()
052 {
053 //If can fire
054 if(!CanFire || !IsEquipped) yield break;
055
056 //Set refire to false
057 CanFire = false;
058
059
//Play Fire Animation
060 gameObject.SendMessage("PlaySpriteAnimation", 0,
SendMessageOptions.DontRequireReceiver);
061
062 //Calculate hit
063
064
//Get ray from screen center target
065 Ray R = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2,
Screen.height/2,0));
066
067 //Test for ray collision
068 RaycastHit hit;
069
070 if(Physics.Raycast(R.origin, R.direction, out hit, Range))
071 {
072 //Target hit - check if target is enemy
073
if(hit.collider.gameObject.CompareTag("enemy"))
074 {
075 //Play collection sound, if audio source is available
076 if(SFX){SFX.PlayOneShot(WeaponAudio, 1.0f);}
077
078
//Send damage message (deal damage to enemy)
079 hit.collider.gameObject.SendMessage("Damage",Damage,
SendMessageOptions.DontRequireReceiver);
080 }
081 }
082
083 //Wait for recovery before re-enabling CanFire
084 yield return new WaitForSeconds(RecoveryDelay);
085
086 //Re-enable CanFire
087 CanFire = true;
088 }