Advertisement
Nikolcho

Player Controller game

Dec 14th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.22 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Timers;
  5. using UnityEngine;
  6.  
  7. public class PlayerController : MonoBehaviour
  8. {
  9.     public const float defaultMS = 10f;
  10.     static Timer timer1;
  11.     public Camera camera;
  12.     public float moveSpeed = 10f;
  13.     public static float MS = 10f;
  14.     public float jumpForce;
  15.     public float raycastDistance;
  16.     private static float time = 5f;
  17.     private static bool speedBoostRanOut;
  18.  
  19.     public Rigidbody playerRigidBody;
  20.     public Transform playerTransform;
  21.  
  22.     bool isCrouchPressed;
  23.     bool isJumpPressed;
  24.  
  25.     bool isCrouching = false;
  26.     bool isStanding = true;
  27.  
  28.     Vector3 tempScale;
  29.     Vector3 curCamHeight;
  30.  
  31.     public Vector3 crouchCamHeight;
  32.  
  33.     void Start()
  34.     {
  35.         camera = Camera.main;
  36.         curCamHeight = camera.transform.position;
  37.     }
  38.  
  39.     void Update()
  40.     {
  41.         isCrouchPressed = Input.GetButtonDown("Crouch");
  42.         isJumpPressed = Input.GetButtonDown("Jump");
  43.  
  44.         float zMovement = Input.GetAxis("Vertical") * moveSpeed;
  45.         float xMovement = Input.GetAxis("Horizontal") * moveSpeed;
  46.  
  47.         zMovement *= Time.deltaTime;
  48.         xMovement *= Time.deltaTime;
  49.  
  50.         playerTransform.Translate(0, 0, zMovement);
  51.         playerTransform.Translate(xMovement, 0, 0);
  52.     }
  53.  
  54.     void FixedUpdate()
  55.     {
  56.         moveSpeed = MS;
  57.         if (isGrounded() && isJumpPressed)
  58.         {
  59.             playerRigidBody.AddForce(0, jumpForce, 0, ForceMode.Impulse);
  60.         }
  61.         if (isCrouchPressed)
  62.         {
  63.             if (isCrouching)
  64.             {
  65.                 Stand();
  66.             }
  67.             else
  68.             {
  69.                 Crouch();
  70.             }
  71.         }
  72.     }
  73.  
  74.     public static void ActivateSpeedBoost(Collision collision)
  75.     {  
  76.         // constant until death
  77.         MS += 2;
  78.         collision.collider.enabled = false;
  79.         collision.collider.transform.position = new Vector3(1, -100, 1);
  80.     }
  81.  
  82.     private void Stand()
  83.     {
  84.         MS *= 2;
  85.         moveSpeed *= 2;
  86.         jumpForce = (jumpForce - 3) * 2;
  87.  
  88.         tempScale = transform.localScale;
  89.         tempScale.y *= 2f;
  90.         transform.localScale = tempScale;
  91.  
  92.         camera.transform.position = new Vector3(camera.transform.position.x,
  93.             camera.transform.position.y + 0.6f,
  94.             camera.transform.position.z);
  95.  
  96.  
  97.         isCrouching = false;
  98.         isStanding = true;
  99.     }
  100.  
  101.     private void Crouch()
  102.     {
  103.         MS /= 2;
  104.         moveSpeed /= 2;
  105.         jumpForce = (jumpForce / 2) + 3;
  106.  
  107.         tempScale = transform.localScale;
  108.         tempScale.y /= 2f;
  109.         transform.localScale = tempScale;
  110.  
  111.         camera.transform.position = new Vector3(camera.transform.position.x,
  112.             camera.transform.position.y - 0.6f,
  113.             camera.transform.position.z);
  114.  
  115.         transform.position = new Vector3(
  116.             transform.position.x,
  117.             transform.position.y - 0.6f,
  118.             transform.position.z);
  119.  
  120.         isCrouching = true;
  121.         isStanding = false;
  122.     }
  123.  
  124.     bool isGrounded()
  125.     {
  126.         return Physics.Raycast(playerTransform.position,
  127.             Vector3.down,
  128.             raycastDistance);
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement