Files
rolld/game/Assets/Scripts/CameraOrbitKeyboard.cs
kerboul a4792759e6 fix: CameraOrbitKeyboard + playersAlive HUD
- CameraOrbitKeyboard: clic droit = unlock, clic gauche = re-lock (coherent avec PlayerController)
- CameraOrbitKeyboard: bloque les inputs quand ChatUI est ouvert
- CameraOrbitKeyboard: OnEnable ne verrouille plus la souris si un panel UI est ouvert
- NetworkManager: alimente GameHUD.SetPlayersAlive via Listen(playersAlive)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-18 08:25:25 +02:00

109 lines
3.4 KiB
C#

using UnityEngine;
using UnityEngine.InputSystem;
using Unity.Cinemachine;
/// <summary>
/// Orbit camera via mouse delta direct (bypass CinemachineInputAxisController)
/// + keyboard fallback. Right-click toggles cursor lock.
/// Active uniquement quand le Player est actif (gameplay).
/// </summary>
public class CameraOrbitKeyboard : MonoBehaviour
{
[Header("Keyboard Orbit Speed (deg/s)")]
public float horizontalSpeed = 150f;
public float verticalSpeed = 80f;
[Header("Mouse Sensitivity (deg/px)")]
public float mouseSensitivity = 0.2f;
private CinemachineOrbitalFollow _orbital;
private CinemachineInputAxisController _axisController;
void Awake()
{
_orbital = GetComponent<CinemachineOrbitalFollow>();
_axisController = GetComponent<CinemachineInputAxisController>();
}
void OnEnable()
{
// On gère la souris nous-mêmes
if (_axisController != null) _axisController.enabled = false;
// Only lock cursor if no UI panel is open
if (!ChatUI.IsVisible && !KeyBindingUI.IsVisible)
LockCursor();
}
void OnDisable()
{
UnlockCursor();
}
void LockCursor()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void UnlockCursor()
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
void Update()
{
if (_orbital == null) return;
var mouse = Mouse.current;
// Right-click unlocks, left-click re-locks (consistent with PlayerController)
if (!ChatUI.IsVisible && !KeyBindingUI.IsVisible && mouse != null)
{
if (Cursor.lockState == CursorLockMode.Locked && mouse.rightButton.wasPressedThisFrame)
UnlockCursor();
else if (Cursor.lockState != CursorLockMode.Locked && mouse.leftButton.wasPressedThisFrame)
LockCursor();
}
if (KeyBindingUI.IsVisible || ChatUI.IsVisible) return;
// Souris — seulement quand locked (delta infini, sans accrochage au bord)
if (Cursor.lockState == CursorLockMode.Locked && mouse != null)
{
Vector2 delta = mouse.delta.ReadValue();
_orbital.HorizontalAxis.Value += delta.x * mouseSensitivity;
_orbital.VerticalAxis.Value = Mathf.Clamp(
_orbital.VerticalAxis.Value - delta.y * mouseSensitivity,
_orbital.VerticalAxis.Range.x,
_orbital.VerticalAxis.Range.y
);
}
// Clavier
var kb = Keyboard.current;
if (kb == null) return;
Key kUp = KeyBindingUI.GetKey("CamUp", Key.W);
Key kDown = KeyBindingUI.GetKey("CamDown", Key.S);
Key kLeft = KeyBindingUI.GetKey("CamLeft", Key.A);
Key kRight = KeyBindingUI.GetKey("CamRight", Key.D);
float h = 0f, v = 0f;
if (kb[kRight].isPressed) h += 1f;
if (kb[kLeft].isPressed) h -= 1f;
if (kb[kUp].isPressed) v += 1f;
if (kb[kDown].isPressed) v -= 1f;
if (h != 0f || v != 0f)
{
_orbital.HorizontalAxis.Value += h * horizontalSpeed * Time.deltaTime;
_orbital.VerticalAxis.Value = Mathf.Clamp(
_orbital.VerticalAxis.Value + v * verticalSpeed * Time.deltaTime,
_orbital.VerticalAxis.Range.x,
_orbital.VerticalAxis.Range.y
);
}
}
}