using UnityEngine; using UnityEngine.InputSystem; namespace NeonTea.Quakeball.Player { /// A controller class for a local player. Handles input and updates the relevant components. [RequireComponent(typeof(Player))] public class LocalPlayer : MonoBehaviour { public Transform Camera; public float PlayerUpdateFrequency = 1; private float Lean = 0; private Player Player; private InputAction LookAction; private InputAction MoveAction; private InputAction CrouchAction; private InputAction JumpAction; private float PreviousPlayerUpdate = -1; private bool WantsToJump = false; private void Awake() { Player = GetComponent(); CrouchAction = new InputAction("crouch", binding: "/buttonEast"); CrouchAction.AddBinding("/leftShift"); CrouchAction.Enable(); JumpAction = new InputAction("crouch", binding: "/buttonSouth"); JumpAction.AddBinding("/space"); JumpAction.performed += _ => { WantsToJump = true; }; JumpAction.Enable(); LookAction = new InputAction("look", binding: "/leftStick"); LookAction.AddBinding("/delta"); LookAction.Enable(); MoveAction = new InputAction("move", binding: "/rightStick"); MoveAction.AddCompositeBinding("Dpad") .With("Up", "/w") .With("Down", "/s") .With("Left", "/a") .With("Right", "/d"); MoveAction.Enable(); } private void Start() { Cursor.visible = false; Cursor.lockState = CursorLockMode.Locked; } private void Update() { OptionsData Opts = Options.Get(); Vector2 LookInput = LookAction.ReadValue() * Opts.LookSensitivity; Player.Pitch = Mathf.Clamp(Player.Pitch - LookInput.y * (Opts.InvertVerticalLook ? -1 : 1), -90, 90); Player.Yaw += LookInput.x; float TargetLean = -Vector3.Dot(Player.GroundVelocity / Player.MoveStyle.TargetVelocity, Camera.right) * Player.MoveStyle.LeanDegrees; Lean = Mathf.Lerp(Lean, TargetLean, 30f * Time.deltaTime); Camera.localEulerAngles = new Vector3(Player.Pitch, Player.Yaw, Lean); if (Time.time - PreviousPlayerUpdate >= 1f / PlayerUpdateFrequency) { Vector2 MovementInput = MoveAction.ReadValue(); Vector3 Move = new Vector3(MovementInput.x, 0, MovementInput.y); Move = Quaternion.Euler(0, Player.Yaw, 0) * Move; Player.MoveDirection = Move; Player.CurrentMoveStyle = CrouchAction.ReadValue() > 0 ? (byte)1 : (byte)0; Player.Jumping = WantsToJump; PreviousPlayerUpdate = Time.time; WantsToJump = false; } } } }