SCTPSController.cs 9.9 KB
Newer Older
Jenny's avatar
Jenny committed
1
2
3
4
5
6
7
8
9
10
11
12
13
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(CharacterController))]

public class SCTPSController : MonoBehaviour
{
    public bool paused;

    public Text QiCount;

Jenny's avatar
Jenny committed
14
    public float speed = 8.0f;
Jenny's avatar
Jenny committed
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
    public float jumpSpeed = 8.0f;
    public float gravity = 30.0f;
    public Transform playerCameraParent;
    public float lookSpeed = 2.0f;
    public float lookXLimit = 60.0f;

    public float moveInAirSpeed = 6.0f;
    public float runSpeed = 9.0f;

    public float glideSpeed = 8.0f;
    public float doubleJumpSpeed = 8.5f;
    public float highJump = 10.0f;

    CharacterController characterController;
    Vector3 moveDirection = Vector3.zero;
    Vector2 rotation = Vector2.zero;

    public float pushPower = 2.0f;

    public int qiThreshold = 3;
    public int qiCollected = 0;
    public bool canGlide;
    public bool canDoubleJump;
    public bool deductQi;

    public int glidesDone;
    public int doubleJumpsDone;

    public int inAir;

    public int jumpsPressed;

    [HideInInspector]
    public bool canMove = true;

    public bool IsGrounded { get; private set; }
    public float ForwardInput { get; set; }
    public float TurnInput { get; set; }
    public bool JumpInput { get; set; }

    new public Rigidbody rigidbody;
    public CapsuleCollider capsuleCollider;

    public float slopeLimit = 45f;
    public bool onGround;

Jenny's avatar
Jenny committed
61
    private Animator anim;
Jenny's avatar
Jenny committed
62
63
64
    public bool amIdle;
    public bool walking;
    public bool running;
Jenny's avatar
Jenny committed
65

Jenny's avatar
Jenny committed
66
    public float jumpLength;
Jenny's avatar
Jenny committed
67
    public bool gliding;
Jenny's avatar
Jenny committed
68

Jenny's avatar
Jenny committed
69
70
71
72
73
    void Start()
    {
        characterController = GetComponent<CharacterController>();
        rotation.y = transform.eulerAngles.y;

Jenny's avatar
Jenny committed
74
75
        anim = GetComponentInChildren<Animator>();

Jenny's avatar
Jenny committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;

        QiCount.text = "Qi Amount: 0";
    }

    void Update()
    {
        if (characterController.isGrounded)
        {
            // We are grounded, so recalculate move direction based on axes
            Vector3 forward = transform.TransformDirection(Vector3.forward);
            Vector3 right = transform.TransformDirection(Vector3.right);
            float curSpeedX = canMove ? speed * Input.GetAxis("Vertical") : 0;
            float curSpeedY = canMove ? speed * Input.GetAxis("Horizontal") : 0;
            moveDirection = (forward * curSpeedX) + (right * curSpeedY);

            if (Input.GetButton("Jump") && canMove)
            {
                moveDirection.y = jumpSpeed;
Jenny's avatar
Jenny committed
96
                jumpLength += 0.1f;
Jenny's avatar
Jenny committed
97
98
            }

99
            if (Input.GetKey(KeyCode.LeftShift))
Jenny's avatar
Jenny committed
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
            {
                moveDirection.x = Input.GetAxis("Horizontal") * runSpeed;
                moveDirection.z = Input.GetAxis("Vertical") * runSpeed;
                moveDirection = transform.TransformDirection(moveDirection);
            }
        }

        if (characterController.isGrounded == false)
        {
            moveDirection.x = Input.GetAxis("Horizontal") * moveInAirSpeed;
            moveDirection.z = Input.GetAxis("Vertical") * moveInAirSpeed;
            moveDirection = transform.TransformDirection(moveDirection);
        }

        // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
        // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
        // as an acceleration (ms^-2)
        moveDirection.y -= gravity * Time.deltaTime;

        // Move the controller
        characterController.Move(moveDirection * Time.deltaTime);

        // Player and Camera rotation
        if (canMove)
        {
            rotation.y += Input.GetAxis("Mouse X") * lookSpeed;
            rotation.x += -Input.GetAxis("Mouse Y") * lookSpeed;
            rotation.x = Mathf.Clamp(rotation.x, -lookXLimit, lookXLimit);
            playerCameraParent.localRotation = Quaternion.Euler(rotation.x, 0, 0);
            transform.eulerAngles = new Vector2(0, rotation.y);
        }

        if (qiCollected >= qiThreshold)
        {
            canGlide = true;
            canDoubleJump = true;
        }

        if (qiCollected < qiThreshold)
        {
            canGlide = false;
            canDoubleJump = false;
            gravity = 30;
        }

        if (canDoubleJump)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                jumpsPressed++;
            }

            if (jumpsPressed >= 6)
            {
               return;
            }

            if (!characterController.isGrounded && Input.GetButtonDown("Jump"))
            {
                moveDirection.y = doubleJumpSpeed;
                doubleJumpsDone += 1;
            }
        }
Jenny's avatar
Jenny committed
163
        
Jenny's avatar
Jenny committed
164
        if (!amIdle && characterController.isGrounded && !Input.GetKey(KeyCode.LeftShift))
165
        {
Jenny's avatar
Jenny committed
166
167
            walking = true;
            running = false;
168
169
            Walk();
        }
Jenny's avatar
Jenny committed
170
        else if (!amIdle && characterController.isGrounded && Input.GetKey(KeyCode.LeftShift))
171
        {
Jenny's avatar
Jenny committed
172
173
            walking = false;
            running = true;
174
175
            Run();
        }
Jenny's avatar
Jenny committed
176
177
178
179
    }

    private void FixedUpdate()
    {
Jenny's avatar
Jenny committed
180
181
182
        if (!Input.GetKey("d") && !Input.GetKey("a") && !Input.GetKey("w") && !Input.GetKey("s"))
        {
            amIdle = true;
Jenny's avatar
Jenny committed
183
            Idle();
Jenny's avatar
Jenny committed
184
185
186
187
188
189
        }
        else
        {
            amIdle = false;
        }

Jenny's avatar
Jenny committed
190
191
192
193
194
195
196
197
198
        if (characterController.isGrounded)
        {
            onGround = true;
        }
        else
        {
            onGround = false;
        }

Jenny's avatar
Jenny committed
199
        if (!gliding && Input.GetButton("Jump"))
Jenny's avatar
Jenny committed
200
201
202
        {
            jumpLength += 1f;
        }
Jenny's avatar
Jenny committed
203
204
205
206
        else if (gliding == true)
        {
            jumpLength = 0;
        }
Jenny's avatar
Jenny committed
207
208
209
210
211
212
213
214
215
216

        if (jumpLength > 0)
        {
            Jump();
        }
        else if (jumpLength == 0)
        {
            anim.SetBool("Jump", false);
        }

Jenny's avatar
Jenny committed
217
218
219
220
221
222
223
224
225
226
227
        CheckGrounded();

        if (canGlide)
        {
            if (Input.GetButton("Jump"))
            {
                inAir++;
            }

            if (!characterController.isGrounded && Input.GetKey(KeyCode.LeftShift))
            {
Jenny's avatar
Jenny committed
228
229
230
231
                gliding = true;

                Glide();

Jenny's avatar
Jenny committed
232
233
234
235
236
237
238
239
240
241
                gravity = 10f;
                glidesDone++;

                moveDirection.x = Input.GetAxis("Horizontal") * glideSpeed;
                moveDirection.z = Input.GetAxis("Vertical") * glideSpeed;
                moveDirection = transform.TransformDirection(moveDirection);
            }
            else
            {
                gravity = 30f;
Jenny's avatar
Jenny committed
242
                gliding = false;
Jenny's avatar
Jenny committed
243
244
            }

245
            if (glidesDone >= 10)
Jenny's avatar
Jenny committed
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
            {
                if (characterController.isGrounded)
                {
                    QiCount.text = "Qi Amount: " + (qiCollected - 3);
                    qiCollected -= 3;
                }
            }
            else
            {
                gravity = 30f;
            }
        }

        if (characterController.isGrounded)
        {
            doubleJumpsDone = 0;
            glidesDone = 0;
            doubleJumpsDone = 0;
            inAir = 0;
            jumpsPressed = 0;
Jenny's avatar
Jenny committed
266
            jumpLength = 0;
Jenny's avatar
Jenny committed
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
        }

        if (doubleJumpsDone >= 2)
        {
            QiCount.text = "Qi Amount: " + (qiCollected - 3);
            qiCollected -= 3;
            doubleJumpsDone -= 1;
        }
    }

    private void CheckGrounded()
    {
        IsGrounded = false;
        float capsuleHeight = Mathf.Max(capsuleCollider.radius * 2f, capsuleCollider.height);
        Vector3 capsuleBottom = transform.TransformPoint(capsuleCollider.center - Vector3.up * capsuleHeight / 2f);
        float radius = transform.TransformVector(capsuleCollider.radius, 0f, 0f).magnitude;

        Ray ray = new Ray(capsuleBottom + transform.up * .01f, -transform.up);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, radius * 5f))
        {
            float normalAngle = Vector3.Angle(hit.normal, transform.up);
            if (normalAngle < slopeLimit)
            {
                float maxDist = radius / Mathf.Cos(Mathf.Deg2Rad * normalAngle) - radius + .02f;
                if (hit.distance < maxDist)
                    IsGrounded = true;
            }
        }
    }

    void OnControllerColliderHit(ControllerColliderHit hit)
    {
        Rigidbody body = hit.collider.attachedRigidbody;

        // no rigidbody
        if (body == null || body.isKinematic)
        {
            return;
        }

        // We dont want to push objects below us
        if (hit.moveDirection.y < -0.3)
        {
            return;
        }

        // Calculate push direction from move direction,
        // we only push objects to the sides never up and down
        Vector3 pushDir = new Vector3(hit.moveDirection.x, 0, hit.moveDirection.z);

        // If you know how fast your character is trying to move,
        // then you can also multiply the push velocity by that.

        // Apply the push
        body.velocity = pushDir * pushPower;
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Qi"))
        {
            qiCollected++;
            Destroy(other.gameObject);

            CheckQiAmount();
        }
    }

    private void CheckQiAmount()
    {
        QiCount.text = "Qi Amount: " + qiCollected;
    }
Jenny's avatar
Jenny committed
340

Jenny's avatar
Jenny committed
341
342
    private void Idle()
    {
Jenny's avatar
Jenny committed
343
        anim.SetFloat("Speed", 0f);
Jenny's avatar
Jenny committed
344
345
    }

Jenny's avatar
Jenny committed
346
347
    private void Walk()
    {
Jenny's avatar
Jenny committed
348
        anim.SetFloat("Speed", 0.333f);
Jenny's avatar
Jenny committed
349
350
351
352
    }

    private void Run()
    {
Jenny's avatar
Jenny committed
353
        anim.SetFloat("Speed", 0.666f);
Jenny's avatar
Jenny committed
354
355
356
357
    }

    private void Jump()
    {
Jenny's avatar
Jenny committed
358
        anim.SetBool("Jump", true);
Jenny's avatar
Jenny committed
359
360
361
362
    }

    private void Glide()
    {
Jenny's avatar
Jenny committed
363
        anim.SetFloat("Speed", 1f);
Jenny's avatar
Jenny committed
364
    }
Jenny's avatar
Jenny committed
365
}