Advertisement
Guest User

GameHandler.cs

a guest
Dec 19th, 2020
34
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Rendering.HighDefinition;
  5. using UnityEngine.InputSystem;
  6.  
  7. public class GameHandler : MonoBehaviour
  8. {
  9.     [Header("GAME SETUP")]
  10.     public CameraHandler cameraFollow;
  11.     public GameObject player;
  12.     public Camera mainCamera;
  13.     public Transform playerTransform;
  14.     public InputMaster controls;
  15.     public GameObject cursor;
  16.  
  17.     [Header("MOVEMENT")]
  18.     public float moveSpeed;
  19.     public float sprintMult;
  20.     public float crouchMult;
  21.  
  22.     [Header("GAMEPLAY")]
  23.     public bool godmode = false;
  24.     public bool infiniteAmmo = true;
  25.     public bool infiniteGrenades = false;
  26.     public bool infiniteAbility = false;
  27.     [Space(10)]
  28.     public Light playerFlashlight;
  29.     public bool infiniteFlashlight = true;
  30.  
  31.     [Header("Display Settings")]
  32.     public int xResolution = 2560;
  33.     public int yResolution = 1440;
  34.     public bool fullscreen = true;
  35.  
  36.     [Header("Camera Settings")]
  37.     public float cameraMoveSpeed = 2f;
  38.     public float cameraZoom = 4f;
  39.     public float cameraZoomSpeed = 1f;
  40.     public float cameraTargetSpeed = .4f;
  41.     public float cameraMaxZoomOut = 10f;
  42.     public float cameraMaxZoomIn = 2f;
  43.     public bool lockMouse = false;
  44.  
  45.     [Header("DEBUG")]
  46.     public bool flashlightEnabled = true;
  47.  
  48.     // INPUT VARIABLES
  49.     public Vector2 movementInput;
  50.     public Vector2 mousePosition;
  51.     public Vector2 mouseScroll;
  52.     bool sprint = false;
  53.     bool crouch = false;
  54.  
  55.     // Awake is called even before Start(), perfect for setting up INPUT controls.
  56.     void Awake()
  57.     {
  58.         // Assign input value to variables
  59.         controls = new InputMaster();
  60.         controls.Movement.Move.performed += ctx => movementInput = ctx.ReadValue<Vector2>();
  61.         controls.Movement.Move.performed += ctx => MovePlayer();
  62.         controls.Movement.Sprint.performed += ctx => sprint = ctx.ReadValueAsButton();
  63.         controls.Movement.Crouch.performed += ctx => crouch = ctx.ReadValueAsButton();
  64.  
  65.         controls.Gameplay.Flashlight.performed += ctx => ToggleFlashlight();
  66.  
  67.         controls.Camera.Mouse.performed += ctx => mousePosition = ctx.ReadValue<Vector2>();
  68.         controls.Camera.Zoom.performed += ctx => mouseScroll = ctx.ReadValue<Vector2>();
  69.         controls.Camera.Zoom.performed += ctx => CameraZoom();
  70.     }
  71.  
  72.     // Start is called before the first frame update.
  73.     void Start()
  74.     {
  75.         Screen.SetResolution(xResolution, yResolution, fullscreen);
  76.         playerTransform = player.transform;
  77.         cameraFollow.Setup(() => playerTransform.position, () => cameraZoom);
  78.         if (lockMouse) Cursor.lockState = CursorLockMode.Locked;
  79.         //cameraFollow.SetGetCameraFollowPositionFunc(() => OTHER.position);
  80.     }
  81.  
  82.     void Update()
  83.     {
  84.         TurnPlayer();
  85.         CameraFollow();
  86.         CursorFollow();
  87.     }
  88.  
  89.     // FixedUpdate is called once per physics update.
  90.     void FixedUpdate()
  91.     {
  92.         float horizontal = movementInput.x;
  93.         float vertical = movementInput.y;
  94.  
  95.         // A key being HELD DOWN is detected using a state check. Check to see if the values are not zero to detect.
  96.         if (movementInput.x != 0 || movementInput.y != 0) MovePlayer();
  97.  
  98.        
  99.  
  100.         // If sprint and crouch keys are pressed simultaneously, cancel them both.
  101.         if (sprint && crouch)
  102.         {
  103.             sprint = false;
  104.             crouch = false;
  105.         }
  106.     }
  107.  
  108.     public void MovePlayer()
  109.     {
  110.         // Since movementInput stores its data in a vector2, it must be converted to vector3.
  111.         // x = x, z = y
  112.         Vector3 move;
  113.         move.x = movementInput.x;
  114.         move.y = 0;
  115.         move.z = movementInput.y;
  116.  
  117.         // While a Rigidbody is set to 'Kinematic', it is no longer affected by forces and fully controlled by scripts.
  118.         /*if (sprint) player.transform.Translate((move * moveSpeed * Time.deltaTime) * sprintMult, Space.World);
  119.         if (crouch) player.transform.Translate((move * moveSpeed * Time.deltaTime) * crouchMult, Space.World);
  120.         else player.transform.Translate(move * moveSpeed * Time.deltaTime, Space.World);*/
  121.  
  122.         Rigidbody rb = player.GetComponent<Rigidbody>();
  123.         Vector3 mov = new Vector3(movementInput.x * moveSpeed, 0, movementInput.y * moveSpeed);
  124.         if (sprint)
  125.         {
  126.             mov.x = mov.x * sprintMult;
  127.             mov.z = mov.z * sprintMult;
  128.         }
  129.         if (crouch)
  130.         {
  131.             mov.x = mov.x * crouchMult;
  132.             mov.z = mov.z * crouchMult;
  133.         }
  134.  
  135.         rb.AddForce(mov.x, mov.y, mov.z);
  136.     }  
  137.  
  138.     public void TurnPlayer()
  139.     {
  140.        
  141.  
  142.         /*Vector3 mouse;
  143.         mouse.x = 0f;
  144.         mouse.y = mousePosition.x;
  145.         mouse.z = 0f;
  146.         Rigidbody rb = player.GetComponent<Rigidbody>();
  147.         float rotationSpeed = 1f;
  148.         rb.rotation = Quaternion.Euler(rb.rotation.eulerAngles + mouse);*/
  149.  
  150.         //Debug.LogWarning(mousePosition);
  151.  
  152.         //Debug.Log(mousePosition);
  153.         //Vector3 target = new Vector3(mousePosition.x, 0, mousePosition.y);
  154.         //Vector3 mouse = mainCamera.ScreenToWorldPoint(Mouse.current.position);
  155.         //mouse.y = 0;
  156.  
  157.         //Quaternion rot = Quaternion.LookRotation(cursor.transform.position);
  158.         //player.GetComponent<Rigidbody>().MoveRotation(rot);
  159.         //Rigidbody rb = player.GetComponent<Rigidbody>();
  160.         //Vector3 pos = new Vector3(cursor.transform.position.x, 0, cursor.transform.position.z);
  161.         //player.transform.LookAt(pos);
  162.         //Quaternion rot = new Quaternion(cursor.transform.position.x, 0, cursor.transform.position.z);
  163.         //rb.MoveRotation
  164.  
  165.         /*Vector3 mouse;
  166.         mouse.x = mousePosition.x;
  167.         mouse.y = 0;
  168.         mouse.z = mousePosition.y;
  169.         Quaternion rot = Quaternion.LookRotation(mouse);
  170.         player.GetComponent<Rigidbody>().MoveRotation(rot);
  171.  
  172.         Vector3 mouse = mainCamera.ScreenToWorldPoint(mousePosition.normalized);
  173.         //
  174.         Vector3 lookDirection = new Vector3(mousePosition.x, 0, mousePosition.y);
  175.         var lookRotation = mainCamera.transform.TransformDirection(lookDirection);
  176.         lookRotation = Vector3.ProjectOnPlane(lookRotation, Vector3.up);
  177.  
  178.         if(lookRotation != Vector3.zero)
  179.         {
  180.             Quaternion newRotation = Quaternion.LookRotation(mouse);
  181.             player.GetComponent<Rigidbody>().MoveRotation(newRotation);
  182.         }*/
  183.     }
  184.  
  185.     public void CursorFollow()
  186.     {
  187.         Vector3 mousePos = new Vector3(Camera.main.ScreenToWorldPoint(mousePosition).x, 0, Camera.main.ScreenToWorldPoint(mousePosition).y);
  188.         cursor.transform.position = mousePos;
  189.  
  190.         Debug.Log(mousePos);
  191.         Debug.LogWarning(mousePosition);
  192.     }
  193.  
  194.     private void OnEnable()
  195.     {
  196.         controls.Enable();
  197.     }
  198.     private void OnDisable()
  199.     {
  200.         controls.Disable();
  201.     }
  202.  
  203.     private void CameraZoom()
  204.     {
  205.         Debug.Log(mouseScroll);
  206.     }
  207.  
  208.     public void CameraFollow()
  209.     {
  210.         Vector3 pos = new Vector3(player.transform.position.x, mainCamera.transform.position.y, player.transform.position.z);
  211.         mainCamera.transform.Translate(pos);
  212.     }
  213.  
  214.     private void ZoomIn()
  215.     {
  216.         cameraZoom -= cameraTargetSpeed;
  217.         if (cameraZoom < cameraMaxZoomIn) cameraZoom = cameraMaxZoomIn;
  218.     }
  219.     private void ZoomOut()
  220.     {
  221.         cameraZoom += cameraTargetSpeed;
  222.         if (cameraZoom > cameraMaxZoomOut) cameraZoom = cameraMaxZoomOut;
  223.     }
  224.  
  225.     private void ToggleFlashlight()
  226.     {
  227.         if (playerFlashlight.enabled) playerFlashlight.enabled = false;
  228.         else if (!playerFlashlight.enabled) playerFlashlight.enabled = true;
  229.     }
  230. }
  231.  
Advertisement
RAW Paste Data Copied
Advertisement