Skip to content
Snippets Groups Projects
Select Git revision
  • master
1 result

README.md

Blame
  • Forked from dst1m17 / comp1314-ppf
    Source project has a limited visibility.
    SCTPSController.cs 8.21 KiB
    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;
                }
    
                if (Input.GetKey(KeyCode.Tab))
                {
                    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;
                }
    
                if (glidesDone >= 3)
                {
                    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;
        }
    }