-
Notifications
You must be signed in to change notification settings - Fork 0
/
FireBlaster.cs
117 lines (96 loc) · 3.2 KB
/
FireBlaster.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using UnityEngine;
using System.Collections;
/// <summary>
/// This script is attached to the player and allows them
/// to fire the projectile
///
/// This script accesses the SpawnSCript
///
/// This script accesses the BlasterScript of a newly instantiated blaster projectile
///
/// All code is taken from Gamer To Game Developer Series 1
/// http://www.gamertogamedeveloper.com
/// No credit is taken myself
///
/// </summary>
public class FireBlaster : MonoBehaviour {
// Variable start_____________________
// The blaster projectile is attached to this in the inspector
public GameObject Blaster;
// Quick references
private Transform myTransform;
private Transform cameraHeadTransform;
// The position at which the projectile should be instantiated
private Vector3 launchPosition = new Vector3();
// Used to control the rate of fire
private float fireRate = 0.2f;
private float nextFire = 0;
// Used to determine which team the player is on
private bool iAmOnTheBlueTeam = false;
private bool iAmOnTheRedTeam = false;
// Variable end_______________________
// Use this for initialization
void Start () {
if(networkView.isMine == true)
{
myTransform = transform;
cameraHeadTransform = myTransform.FindChild("CameraHead");
}
else
{
enabled = false;
}
// Find the SpawnManager and access the SpawnScript to
// find out which team the player is on
GameObject spawnManager = GameObject.Find("SpawnManager");
SpawnScript spawnScript = spawnManager.GetComponent<SpawnScript>();
if(spawnScript.amIOnTheRedTeam == true)
{
iAmOnTheRedTeam = true;
}
if(spawnScript.amIOnTheBlueTeam == true)
{
iAmOnTheBlueTeam = true;
}
}
// Update is called once per frame
void Update () {
if(Input.GetButton("FireWeapon") && Time.time > nextFire && Screen.lockCursor == true)
{
nextFire = Time.time + fireRate;
// The launch position of the projectile will just be
// infront of the CameraHead
launchPosition = cameraHeadTransform.TransformPoint(0, 0, 0.2f);
// Create the blaster projectile accross the network at the launchPosition
// and tilt its angle so that it's horizontal using
// the angle
// Also, make it team specific
if(iAmOnTheRedTeam == true)
{
networkView.RPC("SpawnProjectile", RPCMode.All, launchPosition,
Quaternion.Euler(cameraHeadTransform.eulerAngles.x + 90,
myTransform.eulerAngles.y, 0),
myTransform.name, "red");
}
if(iAmOnTheBlueTeam == true)
{
networkView.RPC("SpawnProjectile", RPCMode.All, launchPosition,
Quaternion.Euler(cameraHeadTransform.eulerAngles.x + 90,
myTransform.eulerAngles.y, 0),
myTransform.name, "blue");
}
}
}
[RPC]
void SpawnProjectile(Vector3 position, Quaternion rotation,
string originatorName, string team)
{
// Access the BlasterScript of the newly instantiated
// Blaster projectile and supply the player's name
// and team
GameObject go = Instantiate(Blaster, position, rotation) as GameObject;
BlasterScript bScript = go.GetComponent<BlasterScript>();
bScript.myOriginator = originatorName;
bScript.team = team;
}
}