-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerCharacter.cs
More file actions
177 lines (143 loc) · 5.09 KB
/
Copy pathPlayerCharacter.cs
File metadata and controls
177 lines (143 loc) · 5.09 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.Collections;
public class PlayerCharacter : MonoBehaviour
{
private CharacterController controller;
private Rigidbody rigidbody;
private bool groundedPlayer;
private Vector3 playerVelocity;
[SerializeField]
private TMP_Text holyTextCounter;
[SerializeField]
private TMP_Text collectedText;
[SerializeField]
private Button myButton;
[SerializeField]
private Material fountainConfirmMaterial;
[SerializeField]
private FountainLife fountainLife;
[SerializeField]
private GenerateHolyText generator;
[SerializeField]
private float playerSpeed = 2.0f;
private float gravityValue = -9.81f;
private int holyTextCount = 0;
[SerializeField]
float timeChange = 10f;
private bool canControl = true;
private void Awake()
{
// always add a controller
controller = GetComponent<CharacterController>();
rigidbody = GetComponent<Rigidbody>();
}
private void Start()
{
holyTextCounter.text = holyTextCount.ToString();
}
public void IncreaseSpeed() {
playerSpeed += 0.5f;
}
// Update is called once per frame
void Update()
{
groundedPlayer = controller.isGrounded;
if (groundedPlayer && playerVelocity.y < 0)
{
playerVelocity.y = 0f;
}
float mouseX = Input.GetAxis("Mouse X") * timeChange;
transform.Rotate(Vector3.up * mouseX);
// Apply gravity
playerVelocity.y += gravityValue * Time.deltaTime;
if (canControl) {
// gather lateral input control
Vector3 moveInput = new Vector3(Input.GetAxis("Horizontal"), playerVelocity.y, Input.GetAxis("Vertical"));
Vector3 move = transform.TransformDirection(moveInput);
// scale by speed
move *= playerSpeed;
// call .Move() once only
controller.Move(move * Time.deltaTime);
Vector3 forward = transform.TransformDirection(Vector3.forward * 10);
Debug.DrawRay(transform.position, forward, Color.green);
}
Debug.DrawRay(transform.position, transform.forward, Color.green);
Debug.DrawRay(transform.position, Vector3.forward, Color.red);
}
void OnTriggerEnter(Collider other)
{
// Get the name of the collided GameObject
string collidedObjectName = other.gameObject.name;
// Output the name to the console
Debug.Log("Collided with: " + collidedObjectName);
if (collidedObjectName == "HolyText") {
Destroy(other.gameObject);
holyTextCount++;
holyTextCounter.text = holyTextCount.ToString();
StartCoroutine(Collected(1.2f));
}
if (collidedObjectName == "Wasp") {
Debug.Log("Add force " + collidedObjectName);
IEnumerator coroutine = GetStung(1.5f);
StartCoroutine(coroutine);
//other.transform.GetComponent<Rigidbody>().AddForce(transform.forward * 2f);
}
}
void OnCollisionEnter(Collision collision)
{
// Get the name of the collided GameObject
string collidedObjectName = collision.gameObject.name;
if (collidedObjectName == "Altar" && holyTextCount > 0) {
for (int i = 0; i < holyTextCount; i++) {
generator.NewHolyText();
}
fountainLife.NewLife(holyTextCount);
holyTextCount = 0;
holyTextCounter.text = holyTextCount.ToString();
StartCoroutine(FountainConfirm(1.2f));
}
}
// every 2 seconds perform the print()
private IEnumerator FountainConfirm(float waitTime)
{
float time = 0f;
float step = 0.01f;
float alpha = 0f;
while (time < waitTime) {
time += Time.deltaTime * 4;
alpha = Mathf.Lerp(1f,0f,(time/waitTime));
Debug.Log(alpha);
fountainConfirmMaterial.color = new Color(255f,255f,255f,alpha);
yield return null;
}
}
// every 2 seconds perform the print()
private IEnumerator Collected(float waitTime)
{
collectedText.enabled = true;
float time = 0f;
float step = 0.01f;
float alpha = 0f;
while (time < waitTime) {
time += Time.deltaTime * 4;
alpha = Mathf.Lerp(1f,0f,(time/waitTime));
Debug.Log(alpha);
collectedText.color = new Color(255f,255f,255f,alpha);
yield return null;
}
collectedText.enabled = false;
}
// every 2 seconds perform the print()
private IEnumerator GetStung(float waitTime)
{
canControl = false;
controller.enabled = false;
transform.GetComponent<Rigidbody>().AddForce(transform.up * 54f);
print("WaitAndPrint " + Time.time);
yield return new WaitForSeconds(waitTime);
canControl = true;
controller.enabled = true;
}
}