using System; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.Serialization; public class BallBehavior : MonoBehaviour { private PlayerInput _playerInput; private InputAction _horizontalAction; private float _horizontalAxis; public int moveForwardSpeed = 3; public float keyboardMoveSpeed = 15f; private int _isKeyboard = 0; private GameObject ball; private float ballRadius; private Vector3 ballBottomPos; private Boolean gameOver = false; // Ground Checks [Header("Ground Checks")] [SerializeField] private bool isGrounded; [SerializeField] private float groundCheckDistance = 0.17f; [SerializeField] private LayerMask groundMask = 0; void Awake() { _playerInput = GetComponent<PlayerInput>(); _horizontalAction = _playerInput.currentActionMap.FindAction("Horizontal"); _horizontalAction.performed += moveActionPerformed; _horizontalAction.canceled += _ => _horizontalAxis = 0; } void Start() { ball = GameObject.Find("Ball"); ballRadius = ball.GetComponent<SphereCollider>().radius; } void Update() { if(!gameOver) { Vector3 position = transform.position; var deltaTime = Time.deltaTime; position += Vector3.forward * (moveForwardSpeed * deltaTime); position += Vector3.right * deltaTime * _horizontalAxis; transform.position = position; } ballBottomPos = ball.transform.position + Vector3.down * ballRadius; isGrounded = Physics.CheckSphere(ballBottomPos, groundCheckDistance, groundMask); Debug.Log(ball.transform.localPosition.y); if(ball.transform.position.y < 0.6f && !isGrounded) { Debug.Log("Game Over: " + ball.transform.position.y + " " + isGrounded); gameOver = true; } } private void moveActionPerformed(InputAction.CallbackContext context) { if (context.control.device.displayName.Contains("Keyboard")) { _isKeyboard = 1; } else { _isKeyboard = 0; } _horizontalAxis = context.ReadValue<float>() * (1 + keyboardMoveSpeed * _isKeyboard); } void OnDrawGizmosSelected() { // Dessine une boule rouge pour visualier goundCheckDistance Gizmos.color = Color.red; Gizmos.DrawSphere(ballBottomPos, groundCheckDistance); } }