Advertisement
dronkowitz

PlayerMovement (Learning Unity).cs

Aug 15th, 2022 (edited)
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.62 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour
  6. {
  7.     public float crawlSpeed = 1f;
  8.     public float crouchSpeed = 2f;
  9.     public float walkSpeed = 3f;
  10.     public float runSpeed = 5f;
  11.     private Vector3 velocity;
  12.     public float jumpHeight;
  13.     public bool isRunning = false;
  14.     public bool crouched = false;
  15.     public bool prone = false;
  16.     public bool canControl
  17.     {
  18.         get
  19.         {
  20.             if (HUD.Instance.conversationScreen.activeSelf) return false;
  21.             return true;
  22.         }
  23.     }
  24.     public Animator anim;
  25.     public bool grounded;
  26.     public LayerMask groundMask;
  27.     public CharacterController body;
  28.     public Transform cameraSystem;
  29.     public Transform head;
  30.     public float currentSpeed
  31.     {
  32.         get
  33.         {
  34.             if (prone)
  35.             {
  36.                 return crawlSpeed;
  37.             }
  38.             if (crouched)
  39.             {
  40.                 return crouchSpeed;
  41.             }
  42.             return isRunning ? runSpeed : walkSpeed;
  43.         }
  44.     }
  45.     // Start is called before the first frame update
  46.     void Start()
  47.     {
  48.         Cursor.lockState = CursorLockMode.Confined;
  49.         Cursor.visible = false;
  50.     }
  51.  
  52.     // Update is called once per frame
  53.     void Update()
  54.     {
  55.         if (!canControl) return;
  56.         Interaction();
  57.         grounded = Physics.CheckSphere(transform.position + (transform.up * (body.radius - 0.1f)), body.radius, groundMask);
  58.         isRunning = Input.GetKey(KeyCode.LeftShift);
  59.  
  60.         if (Input.GetKeyDown(KeyCode.C))
  61.         {
  62.             if (crouched)
  63.             {
  64.                 TryUncrouch();
  65.                
  66.             }
  67.             else
  68.             {
  69.                 if (prone)
  70.                 {
  71.                     float distance = 1.5f;
  72.                     if (!Physics.Raycast(transform.position + Vector3.up * 0.2f, Vector2.up, distance))
  73.                     {
  74.                         prone = false;
  75.                     }
  76.                 }
  77.                 crouched = true;
  78.             }
  79.         }
  80.  
  81.         if (isRunning && crouched)
  82.         {
  83.             TryUncrouch();
  84.         }
  85.  
  86.         if (Input.GetKeyDown(KeyCode.Z))
  87.         {
  88.             if (prone)
  89.             {
  90.  
  91.                 float distance = 1.8f;
  92.                 if (!Physics.Raycast(transform.position + Vector3.up * 0.2f, Vector2.up, distance))
  93.                 {
  94.                     prone = false;
  95.                     crouched = false;
  96.                 }
  97.             }
  98.             else
  99.             {
  100.                 //crouched = false;
  101.                 prone = true;
  102.  
  103.             }
  104.         }
  105.        
  106.         if (grounded && velocity.y < 0)
  107.         {
  108.             velocity.y = -2;
  109.         }
  110.  
  111.         if (Input.GetKeyDown(KeyCode.Space) && grounded && !prone && !crouched)
  112.         {
  113.             velocity.y = Mathf.Sqrt(jumpHeight * -2 * Physics.gravity.y);
  114.         }
  115.        
  116.         UpdateAnimations();
  117.         UpdateHeight();
  118.     }
  119.  
  120.     private void TryUncrouch()
  121.     {
  122.         float distance = 1.8f;
  123.         if (!Physics.Raycast(transform.position + Vector3.up * 0.2f, Vector2.up, distance))
  124.         {
  125.             crouched = false;
  126.             prone = false;
  127.         }
  128.     }
  129.  
  130.     public void UpdateAnimations()
  131.     {
  132.         Vector3 velo = velocity;
  133.         velo.y = 0;
  134.         anim.SetBool("Crouching", crouched);
  135.         anim.SetFloat("SpeedForward", velo.magnitude);
  136.         anim.SetBool("Grounded", grounded);
  137.         anim.SetBool("Prone", prone);
  138.     }
  139.  
  140.     private void FixedUpdate()
  141.     {
  142.         float x = Input.GetAxis("Horizontal");
  143.         float z = Input.GetAxis("Vertical");
  144.  
  145.         Vector3 move = cameraSystem.forward * z + cameraSystem.right * x;
  146.  
  147.         body.Move(move * currentSpeed * Time.deltaTime);
  148.  
  149.         velocity.y += Physics.gravity.y * Time.deltaTime;
  150.         body.Move(velocity * Time.deltaTime);
  151.  
  152.         velocity.x = move.x;
  153.         velocity.z = move.z;
  154.  
  155.         if(move.normalized != Vector3.zero)
  156.             transform.rotation = Quaternion.LookRotation(move.normalized, Vector3.up);
  157.     }
  158.  
  159.     private void UpdateHeight()
  160.     {
  161.         float distance = head.position.y - transform.position.y + 0.2f;
  162.         body.height = distance;
  163.         body.center = Vector3.up * ((distance / 2));
  164.     }
  165.  
  166.     private IInteractable currentInteractable;
  167.     public void Interaction()
  168.     {
  169.         RaycastHit hit;
  170.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  171.         if (Physics.Raycast(ray, out hit, 5f))
  172.         {
  173.             //Debug.Log("Hovering over " + hit.transform.name);
  174.             if (hit.transform.TryGetComponent(out IInteractable other))
  175.             {
  176.                 if (currentInteractable != other)
  177.                 {
  178.                     currentInteractable?.StopHover();
  179.                     currentInteractable = other;
  180.                     currentInteractable.StartHover();
  181.                 }
  182.                 if (other.CanInteract(true))
  183.                 {
  184.                     HUD.Instance.SetCursorType(HUD.CursorTypes.active);
  185.                     if (Input.GetMouseButtonDown(0))
  186.                     {
  187.                         other.Interact(this);
  188.                     }
  189.                     return;
  190.                 }
  191.                 else
  192.                 {
  193.                     HUD.Instance.SetCursorType(HUD.CursorTypes.notActive);
  194.                     return;
  195.                 }
  196.             }
  197.         }
  198.         HUD.Instance.SetCursorType(HUD.CursorTypes.standard);
  199.         currentInteractable?.StopHover();
  200.         currentInteractable = null;
  201.     }
  202. }
  203.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement