fix: caméra — delta souris direct, toggle clic droit, no InputAxisController
- Lit Mouse.current.delta directement (pas d'accrochage au bord d'écran) - CinemachineInputAxisController désactivé (on gère tout nous-mêmes) - OnEnable/OnDisable : lock/unlock automatique selon état Player - Clic droit = toggle cursor lock (pas besoin de maintenir) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -3,53 +3,85 @@ using UnityEngine.InputSystem;
|
|||||||
using Unity.Cinemachine;
|
using Unity.Cinemachine;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds ZQSD (AZERTY) / WASD (QWERTY) keyboard orbit for the Cinemachine camera.
|
/// Orbit camera via mouse delta direct (bypass CinemachineInputAxisController)
|
||||||
/// Works in parallel with mouse orbit via CinemachineInputAxisController.
|
/// + keyboard fallback. Right-click toggles cursor lock.
|
||||||
/// Attach to the CinemachineCamera GameObject alongside CinemachineOrbitalFollow.
|
/// Active uniquement quand le Player est actif (gameplay).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class CameraOrbitKeyboard : MonoBehaviour
|
public class CameraOrbitKeyboard : MonoBehaviour
|
||||||
{
|
{
|
||||||
[Header("Orbit Speed (degrees/sec)")]
|
[Header("Keyboard Orbit Speed (deg/s)")]
|
||||||
public float horizontalSpeed = 150f;
|
public float horizontalSpeed = 150f;
|
||||||
public float verticalSpeed = 80f;
|
public float verticalSpeed = 80f;
|
||||||
|
|
||||||
private CinemachineOrbitalFollow _orbital;
|
[Header("Mouse Sensitivity (deg/px)")]
|
||||||
|
public float mouseSensitivity = 0.2f;
|
||||||
|
|
||||||
|
private CinemachineOrbitalFollow _orbital;
|
||||||
private CinemachineInputAxisController _axisController;
|
private CinemachineInputAxisController _axisController;
|
||||||
|
|
||||||
void Start()
|
void Awake()
|
||||||
{
|
{
|
||||||
_orbital = GetComponent<CinemachineOrbitalFollow>();
|
_orbital = GetComponent<CinemachineOrbitalFollow>();
|
||||||
_axisController = GetComponent<CinemachineInputAxisController>();
|
_axisController = GetComponent<CinemachineInputAxisController>();
|
||||||
if (_orbital == null)
|
}
|
||||||
Debug.LogWarning("[CameraOrbitKeyboard] CinemachineOrbitalFollow not found on this GameObject.");
|
|
||||||
|
void OnEnable()
|
||||||
|
{
|
||||||
|
// On gère la souris nous-mêmes
|
||||||
|
if (_axisController != null) _axisController.enabled = false;
|
||||||
|
LockCursor();
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnDisable()
|
||||||
|
{
|
||||||
|
UnlockCursor();
|
||||||
|
}
|
||||||
|
|
||||||
|
void LockCursor()
|
||||||
|
{
|
||||||
|
Cursor.lockState = CursorLockMode.Locked;
|
||||||
|
Cursor.visible = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void UnlockCursor()
|
||||||
|
{
|
||||||
|
Cursor.lockState = CursorLockMode.None;
|
||||||
|
Cursor.visible = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Update()
|
void Update()
|
||||||
{
|
{
|
||||||
if (_orbital == null) return;
|
if (_orbital == null) return;
|
||||||
|
|
||||||
// Enforce cursor lock while this script is active (Player hierarchy is active = in gameplay)
|
var mouse = Mouse.current;
|
||||||
if (Cursor.lockState != CursorLockMode.Locked)
|
|
||||||
|
// Clic droit = toggle lock
|
||||||
|
if (mouse != null && mouse.rightButton.wasPressedThisFrame)
|
||||||
{
|
{
|
||||||
Cursor.lockState = CursorLockMode.Locked;
|
if (Cursor.lockState == CursorLockMode.Locked)
|
||||||
Cursor.visible = false;
|
UnlockCursor();
|
||||||
|
else
|
||||||
|
LockCursor();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Freeze camera orbit (keyboard + mouse) when keybind menu is open
|
if (KeyBindingUI.IsVisible) return;
|
||||||
if (KeyBindingUI.IsVisible)
|
|
||||||
|
// Souris — seulement quand locked (delta infini, sans accrochage au bord)
|
||||||
|
if (Cursor.lockState == CursorLockMode.Locked && mouse != null)
|
||||||
{
|
{
|
||||||
if (_axisController != null && _axisController.enabled)
|
Vector2 delta = mouse.delta.ReadValue();
|
||||||
_axisController.enabled = false;
|
_orbital.HorizontalAxis.Value += delta.x * mouseSensitivity;
|
||||||
return;
|
_orbital.VerticalAxis.Value = Mathf.Clamp(
|
||||||
}
|
_orbital.VerticalAxis.Value - delta.y * mouseSensitivity,
|
||||||
else if (_axisController != null && !_axisController.enabled)
|
_orbital.VerticalAxis.Range.x,
|
||||||
{
|
_orbital.VerticalAxis.Range.y
|
||||||
_axisController.enabled = true;
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clavier
|
||||||
var kb = Keyboard.current;
|
var kb = Keyboard.current;
|
||||||
if (kb == null) return;
|
if (kb == null) return;
|
||||||
|
|
||||||
// Physical-key mapping: W/A/S/D positions = Z/Q/S/D on AZERTY
|
|
||||||
Key kUp = KeyBindingUI.GetKey("CamUp", Key.W);
|
Key kUp = KeyBindingUI.GetKey("CamUp", Key.W);
|
||||||
Key kDown = KeyBindingUI.GetKey("CamDown", Key.S);
|
Key kDown = KeyBindingUI.GetKey("CamDown", Key.S);
|
||||||
Key kLeft = KeyBindingUI.GetKey("CamLeft", Key.A);
|
Key kLeft = KeyBindingUI.GetKey("CamLeft", Key.A);
|
||||||
@@ -61,10 +93,10 @@ public class CameraOrbitKeyboard : MonoBehaviour
|
|||||||
if (kb[kUp].isPressed) v += 1f;
|
if (kb[kUp].isPressed) v += 1f;
|
||||||
if (kb[kDown].isPressed) v -= 1f;
|
if (kb[kDown].isPressed) v -= 1f;
|
||||||
|
|
||||||
if (Mathf.Abs(h) > 0.001f || Mathf.Abs(v) > 0.001f)
|
if (h != 0f || v != 0f)
|
||||||
{
|
{
|
||||||
_orbital.HorizontalAxis.Value += h * horizontalSpeed * Time.deltaTime;
|
_orbital.HorizontalAxis.Value += h * horizontalSpeed * Time.deltaTime;
|
||||||
_orbital.VerticalAxis.Value = Mathf.Clamp(
|
_orbital.VerticalAxis.Value = Mathf.Clamp(
|
||||||
_orbital.VerticalAxis.Value + v * verticalSpeed * Time.deltaTime,
|
_orbital.VerticalAxis.Value + v * verticalSpeed * Time.deltaTime,
|
||||||
_orbital.VerticalAxis.Range.x,
|
_orbital.VerticalAxis.Range.x,
|
||||||
_orbital.VerticalAxis.Range.y
|
_orbital.VerticalAxis.Range.y
|
||||||
|
|||||||
Reference in New Issue
Block a user