using CustomInspector; using System; using UnityEngine; using Mirror; using…
- User
using CustomInspector; using System; using UnityEngine; using Mirror; using UnityEngine.InputSystem;
namespace PlayerBehaviour { public class PlayerCameraController : CameraController { /// /// (Vertical, Horizontal) /// public readonly static Vector2 defaultLookDirection = new(25, 0); [SerializeField, ReadOnly, Tooltip("(Vertical, Horizontal)")] Vector2 lookDir = Vector2.zero;
float MaxLookHeight => carData.PrefabSettings.maxLookHeight; float MinLookHeight => carData.PrefabSettings.minLookHeight; private float Internal_sensitivity => carData.PrefabSettings.internal_sensitivity; bool rotationBlocked = false; float currentIdle = 0; const float maxIdleTime = 6; //max idle time until looks forward const float idleMoveSpeed = 30; //when idle how fast turned to front private PlayerInputActions playerControls; private Vector2 lastLookInput = Vector2.zero; protected override void Awake() { base.Awake(); lookDir = defaultLookDirection; playerControls = new PlayerInputActions(); // Subscribe to the actions in Awake, if the GameObject is not going to be disabled/enabled often playerControls.Player.Aiming.performed += OnAimPerformed; playerControls.Player.Aiming.canceled += OnAimCanceled; playerControls.Player.Look.canceled += ctx => lastLookInput = ctx.ReadValue<Vector2>(); playerControls.Player.Look.canceled += ctx => lastLookInput = Vector2.zero; } public bool isAiming = false; private void OnAimPerformed(InputAction.CallbackContext context) { isAiming = true; if (!SettingsHandler.IsAimingMethodHold()) { Aiming ^= true; } } private void OnAimCanceled(InputAction.CallbackContext context) { isAiming = false; } private void OnLookPerformed(InputAction.CallbackContext context) { Vector2 lookInput = context.ReadValue<Vector2>(); HandleLook(lookInput); } private void HandleLook(Vector2 lookInput) { // UpdateMouseInput float sensitivity = Internal_sensitivity * SettingsHandler.GetSensitivity(); if (lookInput != Vector2.zero) { lookDir += (sensitivity / currentZoom) * lookInput; currentIdle = 0; } else { //slowly rotate camera/view to car direction if no mouse input if (carData.CameraController.Aiming) { currentIdle = 0; } else { var car = carData.carInstance.CarController; if (car.CurrentAcceleration != Prometeo.CarController.Acceleration.Neutral) //driving { //begin idle when first time steered if (currentIdle > 0) { currentIdle += Time.deltaTime; if (currentIdle > maxIdleTime) { lookDir = Vector2.MoveTowards(lookDir, defaultLookDirection, idleMoveSpeed * Time.deltaTime); } } if (car.CurrentSteering != Prometeo.CarController.Steering.Straight) //if steered { currentIdle += Time.deltaTime; //make it smallest angle if (lookDir.y > 180) lookDir.y -= 360; else if (lookDir.y < -180) lookDir.y += 360; } } } //… existing code … } lookDir.x = Mathf.Clamp(lookDir.x, MinLookHeight, MaxLookHeight); lookDir.y %= 360; } protected override void Start() { base.Start(); if (!isLocalPlayer) return; rotationBlocked = true; Helpers.WaitSeconds(this, 0.5f, () => rotationBlocked = false); } protected override void Update() { base.Update(); if (!isLocalPlayer) return; if (InGameMouse.IsLocked && !rotationBlocked) { HandleLook(lastLookInput); // Pass the last recorded look input to HandleLook // Idle logic if (lastLookInput == Vector2.zero) { // Handle the camera’s idle return-to-center behavior HandleIdleCameraBehavior(); } } //Aiming logic should be handled separately as per your existing logic if (SettingsHandler.IsAimingMethodHold()) { Aiming = isAiming; } } private void HandleIdleCameraBehavior() { // Handle the camera’s idle return-to-center behavior if (!carData.CameraController.Aiming) { var car = carData.carInstance.CarController; if (car.CurrentAcceleration != Prometeo.CarController.Acceleration.Neutral) { // Start idle timer when first time steered currentIdle += Time.deltaTime; if (currentIdle > maxIdleTime) { lookDir = Vector2.MoveTowards(lookDir, defaultLookDirection, idleMoveSpeed * Time.deltaTime); } } if (car.CurrentSteering != Prometeo.CarController.Steering.Straight) { currentIdle += Time.deltaTime; // Normalize the horizontal angle to the (-180, 180) range lookDir.y = lookDir.y < -180 ? lookDir.y + 360 : lookDir.y > 180 ? lookDir.y - 360 : lookDir.y; } } else { currentIdle = 0; } } private void OnEnable() { playerControls.Enable(); // Make sure to enable the input when the object is active } private void OnDisable() { // If we subscribe in Awake, we still need to disable the input when the object is not active playerControls.Disable(); } private void OnDestroy() { if (!this.gameObject.scene.isLoaded) //whole scene was closed return; } } - Assistant
- User
My horizontal and vertical are inverted
- Assistant
Inverted controls in a game, where moving the mouse or a joystick in one direction causes the opposite movement on-screen, can happen due to how the input values are being processed. To correct inverted controls for the horizontal and vertical axes, you can multiply the input values by
-1.In your
HandleLookmethod, where you're updatinglookDirbased onlookInput, you'll want to invert the respective axes:private void HandleLook(Vector2 lookInput) { // UpdateMouseInput float sensitivity = Internal_sensitivity * SettingsHandler.GetSensitivity(); if (lookInput != Vector2.zero) { // If you want to invert the horizontal axis: lookInput.x *= -1; // If you want to invert the vertical axis: lookInput.y *= -1; lookDir += (sensitivity / currentZoom) * lookInput; currentIdle = 0; } // ...rest of the code... }Adjust the
lookInputvector by multiplying either or by to invert that axis before accumulating it to . If both axes are inverted, multiply both values by . - User
- Assistant
With the updated code and the context provided it seems you're suspecting that the horizontal and vertical axes are inverted. Based on the code snippet given, it appears you are already negating
mouseYinput by creating thedeltawithnew(-mouseY, mouseX). This will invert the vertical axis (up-down). For the horizontal axis (left-right), you're currently using the input directly, so if you need to invert it as well, you would negate also.