using System; using UnityEngine.UI; using TMPro; using UnityEngine; using UnityEngine.InputSystem; public class BallController : MonoBehaviour { [SerializeField] private TMP_Text _pickupLeftText; [SerializeField] private TMP_Text _timeSpentText; [SerializeField] private TMP_Text _winText; [SerializeField] Button _playAgain; [SerializeField] Button _quit; int _pickupLeft; float _ballSpeed; float _brakeForce; float _jumpForce; Rigidbody _ball; float _outOfBounds = -1.8f; void Start() { _ball = GetComponent(); _ballSpeed = 3f; _brakeForce = 0.3f; _jumpForce = 14f; _pickupLeft = GameObject.FindGameObjectsWithTag("PickUp").Length; transform.position = new Vector3(0, 0.5f, 0); } void Update() { KeyHandler(); CheckPlayerOut(); UpdateGUI(); } void UpdateGUI() { _pickupLeftText.text = "Left: " + _pickupLeft.ToString(); _timeSpentText.text = "Tid: " + Time.timeSinceLevelLoad.ToString("F1") + "s"; } void OnTriggerEnter(Collider other) { other.gameObject.SetActive(false); _pickupLeft--; if (_pickupLeft == 0) LevelDone(); } void LevelDone() { _pickupLeftText.gameObject.SetActive(false); _timeSpentText.gameObject.SetActive(false); _winText.text = "Snyggt jobbat!\nDin tid: " + Time.timeSinceLevelLoad.ToString("F1") + "s"; _winText.gameObject.SetActive(true); _playAgain.gameObject.SetActive(true); _quit.gameObject.SetActive(true); } void CheckPlayerOut() { if (transform.position.y < _outOfBounds) { transform.position = new Vector3(0, 1f, 0); transform.rotation = Quaternion.Euler(0, 0, 0); } } void KeyHandler() { if (Keyboard.current.wKey.isPressed) { _ball.AddForce(Vector3.forward * _ballSpeed); } else if (Keyboard.current.sKey.isPressed) { _ball.AddForce(Vector3.back * _ballSpeed); } else if (Keyboard.current.dKey.isPressed) { _ball.AddForce(Vector3.right * _ballSpeed); } else if (Keyboard.current.aKey.isPressed) { _ball.AddForce(Vector3.left * _ballSpeed); } if (Keyboard.current.spaceKey.isPressed && transform.position.y < 1) { _ball.AddForce(Vector3.up * _jumpForce); } else { _ball.AddForce(-_ball.linearVelocity * _brakeForce); } } }