using UnityEngine;
///
/// TEMPORAIRE - Debug pour diagnostiquer le problème de chute sous la map.
/// Ajouter sur le prefab PlayerCapsule, supprimer après diagnostic.
///
public class DebugFalling : MonoBehaviour, IClientOnly
{
private CharacterController _cc;
private StarterAssets.FirstPersonController _fpc;
private float _lastLogTime = 0f;
private float _lastY;
private bool _wasFalling = false;
void Start()
{
_cc = GetComponent();
_fpc = GetComponent();
_lastY = transform.position.y;
if (_cc != null)
{
Debug.Log($"[DebugFall] CharacterController: center={_cc.center} height={_cc.height} radius={_cc.radius} skinWidth={_cc.skinWidth}");
Debug.Log($"[DebugFall] Layer joueur: {gameObject.layer} ({LayerMask.LayerToName(gameObject.layer)})");
}
if (_fpc != null)
{
Debug.Log($"[DebugFall] FPC enabled={_fpc.enabled} GroundLayers={_fpc.GroundLayers.value} GroundedOffset={_fpc.GroundedOffset} GroundedRadius={_fpc.GroundedRadius}");
}
// Test : y a-t-il un sol sous nous ?
RaycastHit hit;
if (Physics.Raycast(transform.position + Vector3.up * 0.5f, Vector3.down, out hit, 50f))
Debug.Log($"[DebugFall] Sol détecté: {hit.collider.gameObject.name} layer={LayerMask.LayerToName(hit.collider.gameObject.layer)} distance={hit.distance:F2} point={hit.point}");
else
Debug.Log("[DebugFall] AUCUN SOL DÉTECTÉ sous le joueur !");
// Test GroundLayers
if (_fpc != null)
{
int solLayer = LayerMask.NameToLayer("Sol");
bool solInclus = (_fpc.GroundLayers.value & (1 << solLayer)) != 0;
Debug.Log($"[DebugFall] Layer Sol ({solLayer}) inclus dans GroundLayers: {solInclus}");
// Test CheckSphere comme le fait le FPC
Vector3 spherePos = new Vector3(transform.position.x, transform.position.y - _fpc.GroundedOffset, transform.position.z);
bool grounded = Physics.CheckSphere(spherePos, _fpc.GroundedRadius, _fpc.GroundLayers, QueryTriggerInteraction.Ignore);
Debug.Log($"[DebugFall] CheckSphere grounded={grounded} spherePos={spherePos} radius={_fpc.GroundedRadius}");
}
}
void Update()
{
if (_fpc == null || _cc == null) return;
float y = transform.position.y;
bool falling = y < _lastY - 0.01f;
// Log quand on commence à tomber
if (falling && !_wasFalling)
{
Debug.LogWarning($"[DebugFall] DÉBUT CHUTE à Y={y:F2} pos={transform.position} Grounded={_fpc.Grounded} FPC.enabled={_fpc.enabled} CC.enabled={_cc.enabled} CC.isGrounded={_cc.isGrounded}");
// Vérifier ce qu'il y a sous nous
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.down, out hit, 50f))
Debug.LogWarning($"[DebugFall] Sous nous: {hit.collider.gameObject.name} layer={LayerMask.LayerToName(hit.collider.gameObject.layer)} dist={hit.distance:F2}");
else
Debug.LogWarning("[DebugFall] RIEN sous le joueur !");
}
// Log continu pendant la chute (1x par seconde)
if (falling && Time.time - _lastLogTime > 1f)
{
_lastLogTime = Time.time;
Debug.LogWarning($"[DebugFall] EN CHUTE Y={y:F2} velocity.y={_cc.velocity.y:F2} Grounded={_fpc.Grounded} CC.isGrounded={_cc.isGrounded}");
}
// Log quand on tombe trop bas
if (y < -5f && Time.time - _lastLogTime > 2f)
{
_lastLogTime = Time.time;
Debug.LogError($"[DebugFall] SOUS LA MAP Y={y:F2} téléportation de secours !");
// Téléporter au spawn de secours
_cc.enabled = false;
transform.position = new Vector3(0, 2, 0);
_cc.enabled = true;
}
_wasFalling = falling;
_lastY = y;
}
}