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




Custom Search