using UnityEngine; #if ENABLE_INPUT_SYSTEM using UnityEngine.InputSystem; #endif #if UNITY_EDITOR using UnityEditor; #endif namespace ArcadeVP { public class New_InputManager_ArcadeVP : MonoBehaviour { public ArcadeVehicleController arcadeVehicleController; #if ENABLE_INPUT_SYSTEM [Header("Input Actions")] public InputAction steeringAction; public InputAction accelerationAction; public InputAction brakeAction; public InputAction lookAction; public InputAction lookBehindAction; #endif [Header("Camera Control")] [Tooltip("The parent object of the camera that will rotate when you look around.")] public Transform cameraPivot; public float lookSensitivity = 15f; public float lookReturnSpeed = 5f; private float currentPan = 0f; private float currentTilt = 0f; [Header("Input Smoothing")] public bool useSmoothing = true; public float steeringLerpSpeed = 10f; public float accelerationLerpSpeed = 10f; public float brakeLerpSpeed = 15f; private float currentSteering; private float currentAcceleration; private float currentBrake; private void OnEnable() { #if ENABLE_INPUT_SYSTEM steeringAction?.Enable(); accelerationAction?.Enable(); brakeAction?.Enable(); lookAction?.Enable(); lookBehindAction?.Enable(); #endif } private void OnDisable() { #if ENABLE_INPUT_SYSTEM steeringAction?.Disable(); accelerationAction?.Disable(); brakeAction?.Disable(); lookAction?.Disable(); lookBehindAction?.Disable(); #endif } private void Update() { // Pause controls during UI sequences if (GameManager.Instance != null && !GameManager.Instance.isGameActive) { if (arcadeVehicleController != null) { arcadeVehicleController.ProvideInputs(0f, 0f, 1f); // Slam the brakes } return; } float targetSteering = 0f; float targetAcceleration = 0f; float targetBrake = 0f; #if ENABLE_INPUT_SYSTEM if (steeringAction != null) targetSteering = steeringAction.ReadValue(); if (accelerationAction != null) targetAcceleration = accelerationAction.ReadValue(); if (brakeAction != null) targetBrake = brakeAction.ReadValue(); #else targetSteering = Input.GetAxis("Horizontal"); targetAcceleration = Input.GetAxis("Vertical"); targetBrake = Input.GetAxis("Jump"); #endif if (useSmoothing) { currentSteering = Mathf.Lerp(currentSteering, targetSteering, Time.deltaTime * steeringLerpSpeed); currentAcceleration = Mathf.Lerp(currentAcceleration, targetAcceleration, Time.deltaTime * accelerationLerpSpeed); currentBrake = Mathf.Lerp(currentBrake, targetBrake, Time.deltaTime * brakeLerpSpeed); } else { currentSteering = targetSteering; currentAcceleration = targetAcceleration; currentBrake = targetBrake; } if (arcadeVehicleController != null) { arcadeVehicleController.ProvideInputs(currentSteering, currentAcceleration, currentBrake); } HandleCameraLook(); } private void HandleCameraLook() { if (cameraPivot == null) return; Vector2 lookInput = Vector2.zero; bool lookBehind = false; #if ENABLE_INPUT_SYSTEM if (lookAction != null) lookInput = lookAction.ReadValue(); if (lookBehindAction != null) lookBehind = lookBehindAction.IsPressed(); #else lookInput = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); lookBehind = Input.GetKey(KeyCode.C); #endif if (lookBehind) { // Snap the Cinemachine targets directly to the front (which puts the camera behind looking forward, or vice versa depending on setup, // but rotating 180 degrees perfectly reverses the Follow and LookAt positions!) cameraPivot.localRotation = Quaternion.Euler(0, 180f, 0); currentPan = 180f; currentTilt = 0f; } else if (lookInput.sqrMagnitude > 0.01f) { // Orbit the Cinemachine targets around the car currentPan += lookInput.x * lookSensitivity * Time.deltaTime; currentTilt -= lookInput.y * lookSensitivity * Time.deltaTime; // Clamp the vertical tilt so it doesn't go under the floor or over the roof wildly currentTilt = Mathf.Clamp(currentTilt, -20f, 45f); // Clamp the horizontal pan so they can only look up to 90 degrees left or right // (Looking behind must be done via the button) currentPan = Mathf.Clamp(currentPan, -90f, 90f); cameraPivot.localRotation = Quaternion.Euler(currentTilt, currentPan, 0); } else { // Auto-center the Cinemachine targets back to the default driving view currentPan = Mathf.LerpAngle(currentPan, 0f, Time.deltaTime * lookReturnSpeed); currentTilt = Mathf.LerpAngle(currentTilt, 0f, Time.deltaTime * lookReturnSpeed); cameraPivot.localRotation = Quaternion.Euler(currentTilt, currentPan, 0); } } #if ENABLE_INPUT_SYSTEM public void AddDefaultBindings() { steeringAction = new InputAction("Steering", InputActionType.Value); steeringAction.expectedControlType = "Axis"; steeringAction.AddCompositeBinding("1DAxis") .With("Negative", "/a") .With("Positive", "/d") .With("Negative", "/leftArrow") .With("Positive", "/rightArrow") .With("Negative", "/leftStick/left") .With("Positive", "/leftStick/right"); accelerationAction = new InputAction("Acceleration", InputActionType.Value); accelerationAction.expectedControlType = "Axis"; accelerationAction.AddCompositeBinding("1DAxis") .With("Negative", "/s") .With("Positive", "/w") .With("Negative", "/downArrow") .With("Positive", "/upArrow") .With("Negative", "/leftStick/down") .With("Positive", "/leftStick/up"); brakeAction = new InputAction("Brake", InputActionType.Button); brakeAction.AddBinding("/space") .WithGroup("Keyboard"); brakeAction.AddBinding("/buttonSouth") .WithGroup("Gamepad"); lookAction = new InputAction("Look", InputActionType.Value); lookAction.expectedControlType = "Vector2"; lookAction.AddBinding("/delta"); lookAction.AddBinding("/rightStick"); lookBehindAction = new InputAction("LookBehind", InputActionType.Button); lookBehindAction.AddBinding("/c"); lookBehindAction.AddBinding("/rightStickPress"); } #endif } #if UNITY_EDITOR && ENABLE_INPUT_SYSTEM [CustomEditor(typeof(New_InputManager_ArcadeVP))] public class New_InputManager_ArcadeVP_Editor : Editor { public override void OnInspectorGUI() { DrawDefaultInspector(); New_InputManager_ArcadeVP script = (New_InputManager_ArcadeVP)target; if (GUILayout.Button("Add Default Bindings")) { script.AddDefaultBindings(); EditorUtility.SetDirty(script); } } } #endif }