SCTPSController.cs 8.22 KB
Newer Older
Jenny's avatar
Jenny committed
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
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;

    public float speed = 7.5f;
    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;

    void Start()
    {
        characterController = GetComponent<CharacterController>();
        rotation.y = transform.eulerAngles.y;

        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;

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

    void Update()
    {
        if (paused)
        {
            return;
        }

        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;
            }

93
            if (Input.GetKey(KeyCode.LeftShift))
Jenny's avatar
Jenny committed
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
            {
                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;
            }
        }
    }

    private void FixedUpdate()
    {
        if (characterController.isGrounded)
        {
            onGround = true;
        }
        else
        {
            onGround = false;
        }

        CheckGrounded();

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

            if (!characterController.isGrounded && Input.GetKey(KeyCode.LeftShift))
            {
                gravity = 10f;
                glidesDone++;

                moveDirection.x = Input.GetAxis("Horizontal") * glideSpeed;
                moveDirection.z = Input.GetAxis("Vertical") * glideSpeed;
                moveDirection = transform.TransformDirection(moveDirection);
            }
            else
            {
                gravity = 30f;
            }

193
            if (glidesDone >= 10)
Jenny's avatar
Jenny committed
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
            {
                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;
        }

        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;
    }
}