-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayerManager.cs
More file actions
116 lines (95 loc) · 2.74 KB
/
playerManager.cs
File metadata and controls
116 lines (95 loc) · 2.74 KB
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 System.Collections;
using UnityEngine;
public class playerManager : MonoBehaviour
{
[SerializeField] private healthBar healthBar;
[SerializeField] private GameObject deathPanel;
[Header("Gun types")]
[SerializeField] private GameObject pistolGun;
[SerializeField] private GameObject rifleGun;
private Animator pistolAnimator;
private bool isEquipping;
[SerializeField] private float maxHealth = 100f;
private float currentHealth;
private bool isDead;
private void Start()
{
// initialises variables
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
deathPanel.SetActive(false);
isDead = false;
pistolAnimator = pistolGun.GetComponent<Animator>();
Pistol();
}
private void MyInput() // checks for user input
{
if (Input.GetKey(KeyCode.Alpha1))
{
Rifle();
}
if (Input.GetKey(KeyCode.Alpha2))
{
Pistol();
}
}
public void Pistol()
{
// disables the rifle and activates the pistol
rifleGun.SetActive(false);
pistolGun.SetActive(true);
// plays the animation
if (!isEquipping)
{
// EQUIP trigger activates the animation
pistolAnimator.SetTrigger("EQUIP");
isEquipping = true;
StartCoroutine(WaitForEquipAnimation());
}
}
private IEnumerator WaitForEquipAnimation()
{
// Waits for animation to finish so that it is not repeated
AnimatorStateInfo stateInfo = pistolAnimator.GetCurrentAnimatorStateInfo(0);
yield return new WaitForSeconds(stateInfo.length); // wait for animation
pistolAnimator.SetBool("isEquipped", false); // back to idle
isEquipping = false;
}
public void Rifle()
{
// disables pistol and activates rifle
pistolGun.SetActive(false);
rifleGun.SetActive(true);
}
private void Update()
{
MyInput();
}
public void TakeDamage(float damage)
{
// player damage logic
if (!isDead)
{
currentHealth -= damage;
healthBar.SetHealth(currentHealth);
if (currentHealth <= 0)
{
PlayerDie();
}
}
}
public void PlayerDie()
{
if (!isDead) // If player isn't already dead
{
isDead = true;
Time.timeScale = 0f;
GetComponent<FPScontroller>().enabled = false;
pistolGun.SetActive(false);
rifleGun.SetActive(false);
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
deathPanel.SetActive(true);
}
}
}