1 using UnityEngine;
2 using
System.Collections;
3
4
5 public
class FireGun : MonoBehaviour {
6
7
8     
public GameObject bulletObject;
9     
public float fireRate;
10
11     
float nextFire = 0.0f;
12     GameObject jetType;

13
14     ///
<summary>
15     ///
Find the jetType to which this gun belongs.
16     ///
</summary>
17     
void Start()
18     {
19         
// Is this the player jet or enemy jet?
20         jetType = transform.parent.parent.gameObject;
21
22     }

23
24     ///
<summary>
25     ///
Check whether conditions to fire the gun are satisfied. If they are, consider firing.
26     ///
</summary>
27     
void FixedUpdate () {
28
29         
// If the gun that's about to fire belongs to the player, and the fire button is pressed...
30         
if (jetType.tag == "Player" && Input.GetButton ("Jump"))
31             ConsiderFiring ();
32         
else {
33             
// If this gun doesn't belong to the player, it must belong to an enemy
34             
// Shoot a raycast out from the gun to determine whether its aim is near the player
35             
if (StandardRaycast.GetTagFromRaycast (transform.position, transform.forward, 100000) == "EnemyAI")
36                 ConsiderFiring ();
37
38
39         }
40
41
42     }

43
44     ///
<summary>
45     ///
Check whether we have clock permission to fire. If so, fire and reset clock permission.
46     ///
</summary>
47     
void ConsiderFiring()
48     {
49         
// If we're no longer waiting for clock permission to fire...
50         
if (Time.time > nextFire) {
51
52             
// Create a bullet and give it forward velocity
53             GameObject newBullet = Instantiate (bulletObject, transform.position, transform.rotation)
as GameObject;
54             newBullet.GetComponent<Rigidbody> ().velocity = transform.forward *
500;
55
56             
// Reset clock permission to fire
57             nextFire = Time.time + fireRate;
58
59         }
60     }
61
62
63
64
65 }


Tìm kết quả nhanh