Files
CartoonCopChase/Assets/Ash Assets/Arcade Vehicle Physics/Scripts/New_InputManager_ArcadeVP.cs

224 lines
8.1 KiB
C#
Raw Normal View History

2026-08-28 16:09:06 +01:00
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;
2026-08-28 16:09:06 +01:00
#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;
2026-08-28 16:09:06 +01:00
[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();
2026-08-28 16:09:06 +01:00
#endif
}
private void OnDisable()
{
#if ENABLE_INPUT_SYSTEM
steeringAction?.Disable();
accelerationAction?.Disable();
brakeAction?.Disable();
lookAction?.Disable();
lookBehindAction?.Disable();
2026-08-28 16:09:06 +01:00
#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;
}
2026-08-28 16:09:06 +01:00
float targetSteering = 0f;
float targetAcceleration = 0f;
float targetBrake = 0f;
#if ENABLE_INPUT_SYSTEM
if (steeringAction != null)
targetSteering = steeringAction.ReadValue<float>();
if (accelerationAction != null)
targetAcceleration = accelerationAction.ReadValue<float>();
if (brakeAction != null)
targetBrake = brakeAction.ReadValue<float>();
#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<Vector2>();
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);
}
2026-08-28 16:09:06 +01:00
}
#if ENABLE_INPUT_SYSTEM
public void AddDefaultBindings()
{
steeringAction = new InputAction("Steering", InputActionType.Value);
steeringAction.expectedControlType = "Axis";
steeringAction.AddCompositeBinding("1DAxis")
.With("Negative", "<Keyboard>/a")
.With("Positive", "<Keyboard>/d")
.With("Negative", "<Keyboard>/leftArrow")
.With("Positive", "<Keyboard>/rightArrow")
.With("Negative", "<Gamepad>/leftStick/left")
.With("Positive", "<Gamepad>/leftStick/right");
accelerationAction = new InputAction("Acceleration", InputActionType.Value);
accelerationAction.expectedControlType = "Axis";
accelerationAction.AddCompositeBinding("1DAxis")
.With("Negative", "<Keyboard>/s")
.With("Positive", "<Keyboard>/w")
.With("Negative", "<Keyboard>/downArrow")
.With("Positive", "<Keyboard>/upArrow")
.With("Negative", "<Gamepad>/leftStick/down")
.With("Positive", "<Gamepad>/leftStick/up");
brakeAction = new InputAction("Brake", InputActionType.Button);
brakeAction.AddBinding("<Keyboard>/space")
.WithGroup("Keyboard");
brakeAction.AddBinding("<Gamepad>/buttonSouth")
.WithGroup("Gamepad");
lookAction = new InputAction("Look", InputActionType.Value);
lookAction.expectedControlType = "Vector2";
lookAction.AddBinding("<Pointer>/delta");
lookAction.AddBinding("<Gamepad>/rightStick");
lookBehindAction = new InputAction("LookBehind", InputActionType.Button);
lookBehindAction.AddBinding("<Keyboard>/c");
lookBehindAction.AddBinding("<Gamepad>/rightStickPress");
2026-08-28 16:09:06 +01:00
}
#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
}