using UnityEngine; using UnityEngine.InputSystem; public class dichuyen : MonoBehaviour { // Variables related to player character movement public InputAction MoveAction; Rigidbody2D rigidbody2d; Vector2 move; public float speed = 3.0f; // Variables related to the health system public int maumax = 5; public int health { get { return mauhientai; } } int mauhientai; // Variables related to temporary invincibility public float timeInvincible = 2.0f; bool isInvincible; float damageCooldown; // Start is called once before the first execution of Update after the MonoBehaviour is created //Animation Animator animator; Vector2 moveDirection = new Vector2(1, 0); void Start() { MoveAction.Enable(); rigidbody2d = GetComponent(); animator = GetComponent(); mauhientai = maumax; } // Update is called once per frame void Update() { move = MoveAction.ReadValue(); if (!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f)) { moveDirection.Set(move.x, move.y); moveDirection.Normalize(); } animator.SetFloat("Look X", moveDirection.x); animator.SetFloat("Look Y", moveDirection.y); animator.SetFloat("Speed", move.magnitude); //Debug.Log(move); if (isInvincible) { damageCooldown -= Time.deltaTime; if (damageCooldown < 0) { isInvincible = false; } } } // FixedUpdate has the same call rate as the physics system void FixedUpdate() { Vector2 position = (Vector2)rigidbody2d.position + move * speed * Time.deltaTime; rigidbody2d.MovePosition(position); } public void thaydoimau(int amount) { if (amount < 0) { if (isInvincible) { return; } isInvincible = true; damageCooldown = timeInvincible; animator.SetTrigger("Hit"); } mauhientai = Mathf.Clamp(mauhientai + amount, 0, maumax); Debug.Log(mauhientai + "/" + maumax); } }