Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Rendering.HighDefinition;
- using UnityEngine.InputSystem;
- public class GameHandler : MonoBehaviour
- {
- [Header("GAME SETUP")]
- public CameraHandler cameraFollow;
- public GameObject player;
- public Camera mainCamera;
- public Transform playerTransform;
- public InputMaster controls;
- public GameObject cursor;
- [Header("MOVEMENT")]
- public float moveSpeed;
- public float sprintMult;
- public float crouchMult;
- [Header("GAMEPLAY")]
- public bool godmode = false;
- public bool infiniteAmmo = true;
- public bool infiniteGrenades = false;
- public bool infiniteAbility = false;
- [Space(10)]
- public Light playerFlashlight;
- public bool infiniteFlashlight = true;
- [Header("Display Settings")]
- public int xResolution = 2560;
- public int yResolution = 1440;
- public bool fullscreen = true;
- [Header("Camera Settings")]
- public float cameraMoveSpeed = 2f;
- public float cameraZoom = 4f;
- public float cameraZoomSpeed = 1f;
- public float cameraTargetSpeed = .4f;
- public float cameraMaxZoomOut = 10f;
- public float cameraMaxZoomIn = 2f;
- public bool lockMouse = false;
- [Header("DEBUG")]
- public bool flashlightEnabled = true;
- // INPUT VARIABLES
- public Vector2 movementInput;
- public Vector2 mousePosition;
- public Vector2 mouseScroll;
- bool sprint = false;
- bool crouch = false;
- // Awake is called even before Start(), perfect for setting up INPUT controls.
- void Awake()
- {
- // Assign input value to variables
- controls = new InputMaster();
- controls.Movement.Move.performed += ctx => movementInput = ctx.ReadValue<Vector2>();
- controls.Movement.Move.performed += ctx => MovePlayer();
- controls.Movement.Sprint.performed += ctx => sprint = ctx.ReadValueAsButton();
- controls.Movement.Crouch.performed += ctx => crouch = ctx.ReadValueAsButton();
- controls.Gameplay.Flashlight.performed += ctx => ToggleFlashlight();
- controls.Camera.Mouse.performed += ctx => mousePosition = ctx.ReadValue<Vector2>();
- controls.Camera.Zoom.performed += ctx => mouseScroll = ctx.ReadValue<Vector2>();
- controls.Camera.Zoom.performed += ctx => CameraZoom();
- }
- // Start is called before the first frame update.
- void Start()
- {
- Screen.SetResolution(xResolution, yResolution, fullscreen);
- playerTransform = player.transform;
- cameraFollow.Setup(() => playerTransform.position, () => cameraZoom);
- if (lockMouse) Cursor.lockState = CursorLockMode.Locked;
- //cameraFollow.SetGetCameraFollowPositionFunc(() => OTHER.position);
- }
- void Update()
- {
- TurnPlayer();
- CameraFollow();
- CursorFollow();
- }
- // FixedUpdate is called once per physics update.
- void FixedUpdate()
- {
- float horizontal = movementInput.x;
- float vertical = movementInput.y;
- // A key being HELD DOWN is detected using a state check. Check to see if the values are not zero to detect.
- if (movementInput.x != 0 || movementInput.y != 0) MovePlayer();
- // If sprint and crouch keys are pressed simultaneously, cancel them both.
- if (sprint && crouch)
- {
- sprint = false;
- crouch = false;
- }
- }
- public void MovePlayer()
- {
- // Since movementInput stores its data in a vector2, it must be converted to vector3.
- // x = x, z = y
- Vector3 move;
- move.x = movementInput.x;
- move.y = 0;
- move.z = movementInput.y;
- // While a Rigidbody is set to 'Kinematic', it is no longer affected by forces and fully controlled by scripts.
- /*if (sprint) player.transform.Translate((move * moveSpeed * Time.deltaTime) * sprintMult, Space.World);
- if (crouch) player.transform.Translate((move * moveSpeed * Time.deltaTime) * crouchMult, Space.World);
- else player.transform.Translate(move * moveSpeed * Time.deltaTime, Space.World);*/
- Rigidbody rb = player.GetComponent<Rigidbody>();
- Vector3 mov = new Vector3(movementInput.x * moveSpeed, 0, movementInput.y * moveSpeed);
- if (sprint)
- {
- mov.x = mov.x * sprintMult;
- mov.z = mov.z * sprintMult;
- }
- if (crouch)
- {
- mov.x = mov.x * crouchMult;
- mov.z = mov.z * crouchMult;
- }
- rb.AddForce(mov.x, mov.y, mov.z);
- }
- public void TurnPlayer()
- {
- /*Vector3 mouse;
- mouse.x = 0f;
- mouse.y = mousePosition.x;
- mouse.z = 0f;
- Rigidbody rb = player.GetComponent<Rigidbody>();
- float rotationSpeed = 1f;
- rb.rotation = Quaternion.Euler(rb.rotation.eulerAngles + mouse);*/
- //Debug.LogWarning(mousePosition);
- //Debug.Log(mousePosition);
- //Vector3 target = new Vector3(mousePosition.x, 0, mousePosition.y);
- //Vector3 mouse = mainCamera.ScreenToWorldPoint(Mouse.current.position);
- //mouse.y = 0;
- //Quaternion rot = Quaternion.LookRotation(cursor.transform.position);
- //player.GetComponent<Rigidbody>().MoveRotation(rot);
- //Rigidbody rb = player.GetComponent<Rigidbody>();
- //Vector3 pos = new Vector3(cursor.transform.position.x, 0, cursor.transform.position.z);
- //player.transform.LookAt(pos);
- //Quaternion rot = new Quaternion(cursor.transform.position.x, 0, cursor.transform.position.z);
- //rb.MoveRotation
- /*Vector3 mouse;
- mouse.x = mousePosition.x;
- mouse.y = 0;
- mouse.z = mousePosition.y;
- Quaternion rot = Quaternion.LookRotation(mouse);
- player.GetComponent<Rigidbody>().MoveRotation(rot);
- Vector3 mouse = mainCamera.ScreenToWorldPoint(mousePosition.normalized);
- //
- Vector3 lookDirection = new Vector3(mousePosition.x, 0, mousePosition.y);
- var lookRotation = mainCamera.transform.TransformDirection(lookDirection);
- lookRotation = Vector3.ProjectOnPlane(lookRotation, Vector3.up);
- if(lookRotation != Vector3.zero)
- {
- Quaternion newRotation = Quaternion.LookRotation(mouse);
- player.GetComponent<Rigidbody>().MoveRotation(newRotation);
- }*/
- }
- public void CursorFollow()
- {
- Vector3 mousePos = new Vector3(Camera.main.ScreenToWorldPoint(mousePosition).x, 0, Camera.main.ScreenToWorldPoint(mousePosition).y);
- cursor.transform.position = mousePos;
- Debug.Log(mousePos);
- Debug.LogWarning(mousePosition);
- }
- private void OnEnable()
- {
- controls.Enable();
- }
- private void OnDisable()
- {
- controls.Disable();
- }
- private void CameraZoom()
- {
- Debug.Log(mouseScroll);
- }
- public void CameraFollow()
- {
- Vector3 pos = new Vector3(player.transform.position.x, mainCamera.transform.position.y, player.transform.position.z);
- mainCamera.transform.Translate(pos);
- }
- private void ZoomIn()
- {
- cameraZoom -= cameraTargetSpeed;
- if (cameraZoom < cameraMaxZoomIn) cameraZoom = cameraMaxZoomIn;
- }
- private void ZoomOut()
- {
- cameraZoom += cameraTargetSpeed;
- if (cameraZoom > cameraMaxZoomOut) cameraZoom = cameraMaxZoomOut;
- }
- private void ToggleFlashlight()
- {
- if (playerFlashlight.enabled) playerFlashlight.enabled = false;
- else if (!playerFlashlight.enabled) playerFlashlight.enabled = true;
- }
- }
Advertisement
RAW Paste Data
Copied
Advertisement