-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeaponSystem.cs
108 lines (91 loc) · 3.01 KB
/
WeaponSystem.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
/// <summary>
/// WeaponSystem.cs
/// Author: MutantGopher
/// This script manages weapon switching. It's recommended that you attach this to a parent GameObject of all your weapons, but this is not necessary.
/// This script allows the player to switch weapons in two ways, by pressing the numbers corresponding to each weapon, or by scrolling with the mouse.
/// </summary>
namespace VRTK.Fun
{
using UnityEngine;
using System.Collections;
public class WeaponSystem : MonoBehaviour
{
public GameObject[] weapons; // The array that holds all the weapons that the player has
public int startingWeaponIndex = 0; // The weapon index that the player will start with
private int weaponIndex; // The current index of the active weapon
// Use this for initialization
void Start()
{
// Make sure the starting active weapon is the one selected by the user in startingWeaponIndex
weaponIndex = startingWeaponIndex;
SetActiveWeapon(weaponIndex);
}
// Update is called once per frame
void FixedUpdate()
{
// Allow the user to instantly switch to any weapon
// if (Input.GetButtonDown("Weapon 1"))
// SetActiveWeapon(0);
// if (Input.GetButtonDown("Weapon 2"))
// SetActiveWeapon(1);
// if (Input.GetButtonDown("Weapon 3"))
// SetActiveWeapon(2);
// if (Input.GetButtonDown("Weapon 4"))
// SetActiveWeapon(3);
// if (Input.GetButtonDown("Weapon 5"))
// SetActiveWeapon(4);
// if (Input.GetButtonDown("Weapon 6"))
// SetActiveWeapon(5);
// if (Input.GetButtonDown("Weapon 7"))
// SetActiveWeapon(6);
// if (Input.GetButtonDown("Weapon 8"))
// SetActiveWeapon(7);
// if (Input.GetButtonDown("Weapon 9"))
// SetActiveWeapon(8);
// Allow the user to scroll through the weapons
if (Input.GetAxis("Mouse ScrollWheel") > 0)
NextWeapon();
if (Input.GetAxis("Mouse ScrollWheel") < 0)
PreviousWeapon();
}
void OnGUI()
{
}
public void SetActiveWeapon(int index)
{
// Make sure this weapon exists before trying to switch to it
if (index >= weapons.Length || index < 0)
{
Debug.LogWarning("Tried to switch to a weapon that does not exist. Make sure you have all the correct weapons in your weapons array.");
return;
}
// Send a messsage so that users can do other actions whenever this happens
SendMessageUpwards("OnEasyWeaponsSwitch", SendMessageOptions.DontRequireReceiver);
// Make sure the weaponIndex references the correct weapon
weaponIndex = index;
// Make sure beam game objects aren't left over after weapon switching
weapons[index].GetComponent<Weapon>().StopBeam();
// Start be deactivating all weapons
for (int i = 0; i < weapons.Length; i++)
{
weapons[i].SetActive(false);
}
// Activate the one weapon that we want
weapons[index].SetActive(true);
}
public void NextWeapon()
{
weaponIndex++;
if (weaponIndex > weapons.Length - 1)
weaponIndex = 0;
SetActiveWeapon(weaponIndex);
}
public void PreviousWeapon()
{
weaponIndex--;
if (weaponIndex < 0)
weaponIndex = weapons.Length - 1;
SetActiveWeapon(weaponIndex);
}
}
}