using UnityEngine; using UnityEngine.UI; public class DogController : MonoBehaviour { [Header("Movement")] public float moveSpeed = 6f; public float jumpForce = 6f; public float groundCheckDistance = 0.55f; public LayerMask groundMask; private Rigidbody rb; private int coins = 0; private Text coinsText; void Start() { rb = GetComponent(); if (rb == null) Debug.LogError("DogController precisa de um Rigidbody."); // Cria um contador de moedas (UI Text) no canto superior direito GameObject canvasGO = new GameObject("DogHUD_Canvas"); var canvas = canvasGO.AddComponent(); canvas.renderMode = RenderMode.ScreenSpaceOverlay; canvasGO.AddComponent(); canvasGO.AddComponent(); GameObject textGO = new GameObject("CoinsText"); textGO.transform.SetParent(canvasGO.transform); coinsText = textGO.AddComponent(); coinsText.font = Resources.GetBuiltinResource("Arial.ttf"); coinsText.fontSize = 28; coinsText.alignment = TextAnchor.UpperRight; coinsText.text = "Ossos: 0"; // Posiciona o Text no canto superior direito com margens RectTransform rt = coinsText.GetComponent(); rt.anchorMin = new Vector2(1, 1); // canto superior direito rt.anchorMax = new Vector2(1, 1); rt.pivot = new Vector2(1, 1); rt.anchoredPosition = new Vector2(-16, -16); // 16px de margem do canto rt.sizeDelta = new Vector2(300, 60); } void Update() { HandleMovementInput(); } void HandleMovementInput() { // Lê WASD como requisitado float h = 0f, v = 0f; if (Input.GetKey(KeyCode.W)) v += 1f; if (Input.GetKey(KeyCode.S)) v -= 1f; if (Input.GetKey(KeyCode.D)) h += 1f; if (Input.GetKey(KeyCode.A)) h -= 1f; Vector3 localMove = new Vector3(h, 0f, v).normalized; // direções locais Vector3 worldMove = transform.TransformDirection(localMove) * moveSpeed; // Mantém componente Y atual (para não sobrescrever pulo) rb.velocity = new Vector3(worldMove.x, rb.velocity.y, worldMove.z); // Pulo — só quando estiver em contato com o chão if (Input.GetKeyDown(KeyCode.Space) && IsGrounded()) { rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse); } } bool IsGrounded() { // Raycast curto para checar se está perto do chão return Physics.Raycast(transform.position, Vector3.down, groundCheckDistance, groundMask); } // Coleta de ossos (assumindo que os ossos têm Collider com isTrigger = true) private void OnTriggerEnter(Collider other) { if (other.CompareTag("Bone")) { coins++; UpdateCoinsUI(); Destroy(other.gameObject); } } private void UpdateCoinsUI() { if (coinsText != null) coinsText.text = "Ossos: " + coins; } }

Enjoying this? A quick like helps keep it online longer.

Content Expiring Soon

This content will be deleted in less than 24 hours. If you like it, you can extend its lifetime to keep it available.

0 likes
0 views
12 days left
Like what you see? Create your own
0
0
12d