-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerController.cs
219 lines (177 loc) · 6.42 KB
/
PlayerController.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{
//NOTES
// -Discs that spin, requiring you to run the opposite way (pepermint)
// -Jolly Rancher jumps (similar to MC.Honey) that make it so you can't jump
// -Jello that bounces you around
// -Pretzal hoops (maybe they spin?)
//KNOWN BUGS
// -the ball does not move with the spining peppermint, currently cant figure out how to fix this
// -Sometimes velocity on the ball completely changes, going from very fast to very slow
//================VARIABLES================
//Movement
// X/Z Movement
public float speed = 1;
public Rigidbody rb;
//Y Movement aka jumping
public int jumpspeed = 1;
public bool isGrounded = true;
public int ground;
//Avoiding double jumping
public SphereCollider col;
//Level Boundaries
//Reseting when falling too far
public float threshhold = -6;
public float resetHeight = -5;
//Level Identification
public int level = 0;
//Scoring
public int score;
public TextMeshProUGUI scoreText;
//Win Text
public GameObject winTextObject;
//Checkpoints
public static Vector3 LastCheckPointPos = new Vector3(0,2,0);
public GameObject controlInfo;
//================START================
void Start()
{
rb = GetComponent<Rigidbody>();
col = GetComponent<SphereCollider>();
score = 0;
SetScoreText ();
winTextObject.SetActive(false);
controlInfo.SetActive(false);
}
//================UPDATE================
void Update()
{
//Movement - X/Z Axis
float movementHorizontal = Input.GetAxis ("Horizontal");
float movementVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (movementHorizontal, 0.0f, movementVertical);
//rb.velocity = rb.velocity.normalized * speed;
rb.AddForce(movement * speed);
//Jumping
//Need to set seperately since IsGrounded is a boolean and Input is a multifunctional
if (isGrounded == true)
{
if(Input.GetKey(KeyCode.Space))
{
Vector3 jump = new Vector3(0.0f, 10.0f, 0.0f);
rb.AddForce(jump * jumpspeed);
}
}
//Falling off the map restarts you in the test room
if(transform.position.y < threshhold)
{
//Resetting to the last known checkpoint
Invoke("DeathMovement", 0.0f);
GameObject.FindGameObjectWithTag("Player").transform.position = LastCheckPointPos;
//Backup
//Application.LoadLevel(Application.loadedLevel);
}
//Win Index
//Collect all available 'Coins' and reach final level
if (level == 3)
{
// Set the text value of your 'winText'
winTextObject.SetActive(true);
}
//DEBUG Menu show/hide
//Show
if(Input.GetKeyDown(KeyCode.P))
{
controlInfo.gameObject.SetActive(true);
}
//Hide
if(Input.GetKeyUp(KeyCode.P))
{
controlInfo.gameObject.SetActive(false);
}
//-----DEBUG//DEV CODES-----
//THERE IS A BUG WITH THE BALL SPEEDS IN UNITY VS BUILDS, FOR UNITY THE SPEED & JUMP SPEED SHOULD BE 2 BUT ANY
//BUILD SHOULD HAVE A SPEED & JUMP SPEED OF 10 & 20
//Editor is 5 & 6
//If Keypad Plus is pressed - Increase Score
if(Input.GetKeyDown(KeyCode.KeypadPlus))
{
score = score + 1;
SetScoreText();
}
//If Keypad Enter is pressed - Move ball up 10
if(Input.GetKeyDown(KeyCode.KeypadEnter))
{
transform.position = new Vector3(transform.position.x, transform.position.y+10, transform.position.z);
}
//If Keypad 0-3 pressed - restart at Test Screen-lvl 3
if(Input.GetKey(KeyCode.Keypad0))
{
transform.position = new Vector3(0, 2, 0);
}
if(Input.GetKey(KeyCode.Keypad1))
{
transform.position = new Vector3(0, 2, 95);
}
if(Input.GetKey(KeyCode.Keypad2))
{
transform.position = new Vector3(0, 20, 130);
}
if(Input.GetKey(KeyCode.Keypad3))
{
transform.position = new Vector3(0, 39, 173);
}
//If Escape is pressed - Leave Game
if(Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();
}
//If Keypad Period is pressed - Warp to last checkpoint
if(Input.GetKey(KeyCode.KeypadPeriod))
{
GameObject.FindGameObjectWithTag("Player").transform.position = LastCheckPointPos;
}
}
//================OTHER FUNCTIONS================
//Reducing the velocity of the ball upon falling out of the world/dying
void DeathMovement()
{
rb.velocity = Vector3.one;
//rb.angularVelocity = Vector3.one;
Debug.Log("Death Reset");
}
//JUMPING
//GROUND LAYERS - ENTER & EXIT
void OnCollisionEnter(Collision Ground)
{
if(Ground.gameObject.tag == "Ground")
{
isGrounded = true;
}
}
void OnCollisionExit(Collision Ground)
{
if(Ground.gameObject.tag == "Ground")
{
isGrounded = false;
}
}
public void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("Pickup"))
{
other.gameObject.SetActive(false);
score = score + 1;
SetScoreText ();
}
}
void SetScoreText()
{
scoreText.text = "Score: " + score.ToString();
}
}