using UnityEngine; using UnityEngine.InputSystem; public class PlayerController : MonoBehaviour { public CharacterController charCon; public WeaponsController weaponCon; // Reference to the WeaponsController script public float moveSpeed, lookSpeed, jumpPower, runSpeed, camZoomNormal, camZoomOut, camZoomSpeed, minViewAngle, maxViewAngle; public InputActionReference moveAction, lookAction, jumpAction, sprintAction, shootAction, reloadAction; // Input actions for movement, looking, jumping, sprinting, shooting, and reloading private Vector3 currentMovement; private Vector2 rotStore; public Camera theCam; public float gravityModifier = 2f; // Optional gravity modifier for additional control // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { Cursor.lockState = CursorLockMode.Locked; } // Update is called once per frame void Update() { float yStore = currentMovement.y; // Store the current vertical velocity (e.g., gravity) //Handle Movement Vector2 moveInput = moveAction.action.ReadValue(); //currentMovement = new Vector3(moveInput.x * moveSpeed, 0f, moveInput.y * moveSpeed); Vector3 moveForward = transform.forward * moveInput.y; Vector3 moveSideways = transform.right * moveInput.x; //Handle Sprinting if (sprintAction.action.IsPressed() && charCon.isGrounded) // Check if sprint action is pressed and character is grounded { currentMovement = (moveForward + moveSideways).normalized * runSpeed; // Increase speed when sprinting if (currentMovement != Vector3.zero) // Only zoom out if there is movement { theCam.fieldOfView = Mathf.Lerp(theCam.fieldOfView, camZoomOut, camZoomSpeed * Time.deltaTime); // Zoom out the camera when sprinting } } else { currentMovement = (moveForward + moveSideways).normalized * moveSpeed; // Normal movement speed theCam.fieldOfView = Mathf.Lerp(theCam.fieldOfView, camZoomNormal, camZoomSpeed * Time.deltaTime); // Reset camera zoom when not sprinting } //currentMovement = (moveForward + moveSideways).normalized * moveSpeed; //Handle Gravity if (charCon.isGrounded) { yStore = 0f; // Reset vertical velocity when grounded } currentMovement.y = yStore + (Physics.gravity.y * Time.deltaTime * gravityModifier); // Preserve vertical velocity (e.g., gravity) // Handle Jumping if (jumpAction.action.WasPressedThisFrame() && charCon.isGrounded) // Check if jump action is pressed and character is grounded { currentMovement.y = jumpPower; // Apply jump power when the jump action is pressed } charCon.Move(currentMovement * Time.deltaTime); //Handle Looking Vector2 lookInput = lookAction.action.ReadValue(); lookInput.y = -lookInput.y; // Invert the Y-axis for looking up and down rotStore = rotStore + (lookInput * lookSpeed * Time.deltaTime); rotStore.y = Mathf.Clamp(rotStore.y, minViewAngle, maxViewAngle); transform.rotation = Quaternion.Euler(0f, rotStore.x, 0f); theCam.transform.localRotation = Quaternion.Euler(rotStore.y, 0f, 0f); //Handle Shooting if (shootAction.action.WasPressedThisFrame()) { weaponCon.Shoot(); // Call the Shoot method from WeaponsController when the shoot action is pressed } if (shootAction.action.IsPressed() && weaponCon.canAutoFire) // Check if shoot action is held and auto fire is enabled { weaponCon.ShootHeld(); // Call the ShootHeld method from WeaponsController for continuous shooting } //Handle Reloading if (reloadAction.action.WasPressedThisFrame()) { weaponCon.Reload(); // Call the Reload method from WeaponsController when the reload action is pressed } } }