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 float mouseX; private float mouseY; 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.MouseX.performed += OnMouseXPerformed; playerControls.Player.MouseX.canceled += OnMouseXCanceled; playerControls.Player.MouseY.performed += OnMouseYPerformed; playerControls.Player.MouseY.canceled += OnMouseYCanceled; } 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 OnMouseXPerformed(InputAction.CallbackContext context) { mouseX = context.ReadValue<float>(); } private void OnMouseXCanceled(InputAction.CallbackContext context) { mouseX = 0f; } private void OnMouseYPerformed(InputAction.CallbackContext context) { mouseY = context.ReadValue<float>(); } private void OnMouseYCanceled(InputAction.CallbackContext context) { mouseY = 0f; } protected override void Start() { base.Start(); if (!isLocalPlayer) return; rotationBlocked = true; Helpers.WaitSeconds(this, 0.5f, () => rotationBlocked = false); } protected override void Update() { //Base base.Update(); if (!isLocalPlayer) return; //Turning if (InGameMouse.IsLocked // not rotate when in some kind of menu where cursor is not locked && !rotationBlocked) // only start rotating a bit after the start, so user feels position before moving { // UpdateMouseInput float sensitivity = Internal_sensitivity * SettingsHandler.GetSensitivity() * 0.08f; Vector2 delta = new(-mouseY, mouseX); if (delta != Vector2.zero) { lookDir += (sensitivity / currentZoom) * delta; 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; } } } } lookDir.x = Mathf.Clamp(lookDir.x, MinLookHeight, MaxLookHeight); lookDir.y %= 360; } LookDirection.LocalEulerAngles = lookDir; //Aiming if (SettingsHandler.IsAimingMethodHold()) Aiming = isAiming; } 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
Can you adjust my script so that it uses the stick values (-1 to 1)?
- Assistant
Certainly! To use the stick values directly from the input system, which generally range from -1 to 1 for both axes, you can simply map these values to your camera movement. Here's an adjusted version of your
PlayerCameraControllerscript where we handle the stick inputs for camera movement:// ... [rest of your class code above] private void OnMouseXPerformed(InputAction.CallbackContext context) { // Right stick horizontal value from controller, typically in the range of -1 to 1 mouseX = context.ReadValue<float>(); } private void OnMouseYPerformed(InputAction.CallbackContext context) { // Right stick vertical value from controller, typically in the range of -1 to 1 mouseY = context.ReadValue<float>(); } protected override void Update() { // ... [rest of your update code] if (!isLocalPlayer || !InGameMouse.IsLocked || rotationBlocked) return; // Apply sensitivity to stick values and update the look direction float sensitivity = Internal_sensitivity * SettingsHandler.GetSensitivity() * 0.08f; Vector2 delta = new(-mouseY * sensitivity, mouseX * sensitivity); // ... [rest of your update code] // Reset the mouseX and mouseY values at the end of each frame to avoid accumulating input from the previous frame mouseX = 0f; mouseY = 0f; } // ... [rest of your class code below]