using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.UI; public class FightingController : MonoBehaviour { [Header("Player Movement")] public float movementSpeed = 1f; public float rotationSpeed = 10f; private CharacterController characterController; private Animator animator; public Joystick joystick; [Header("Player Fight")] public float attackCooldown = 0.5f; public int attackDamage = 5; public string[] attackAnimations = { "Attack1Animation", "Attack2Animation", "Attack3Animation", "Attack4Animation" }; public float dodgeDistance = 2f; public float attackRadius = 2.2f; public Transform[] opponents; private float lastAttackTime; [Header("Effects and sounds")] public ParticleSystem attack1Effect; public ParticleSystem attack2Effect; public ParticleSystem attack3Effect; public ParticleSystem attack4Effect; public AudioClip[] hitSounds; [Header("Health")] public int maxHealth = 100; public int currentHealth; public HealthBar healthBar; void Awake() { currentHealth = maxHealth; healthBar.GiveFullHealth(currentHealth); characterController = GetComponent<CharacterController>(); animator = GetComponent<Animator>(); } void Update() { PerformMovement(); PerformDodgeFront(); if (Input.GetKeyDown(KeyCode.Alpha1)) { PerformAttack(0); } else if (Input.GetKeyDown(KeyCode.Alpha2)) { PerformAttack(1); } else if (Input.GetKeyDown(KeyCode.Alpha3)) { PerformAttack(2); } else if (Input.GetKeyDown(KeyCode.Alpha4)) { PerformAttack(3); } } void PerformMovement() { float horizontal = Input.GetAxis("Horizontal") + joystick.Horizontal; float vertical = Input.GetAxis("Vertical") + joystick.Vertical; Vector3 movement = new Vector3(-vertical, 0f, horizontal); if (movement != Vector3.zero) { Quaternion targetRotation = Quaternion.LookRotation(movement); transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime); animator.SetBool("Walking", true); } else { animator.SetBool("Walking", false); } characterController.Move(movement * movementSpeed * Time.deltaTime); } void PerformAttack(int attackIndex) { if (Time.time - lastAttackTime > attackCooldown) { animator.Play(attackAnimations[attackIndex]); int damage = attackDamage; Debug.Log("Performed attack" + (attackIndex + 1) + "dealing" + damage + "damage"); lastAttackTime = Time.time; //Loop through each opponent foreach (Transform opponent in opponents) { if (Vector3.Distance(transform.position, opponent.position) <= attackRadius) { opponent.GetComponent<OpponentAI>().StartCoroutine(opponent.GetComponent<OpponentAI>().PlayHitDamageAnimation(attackDamage)); } } } else { Debug.Log("Endlag"); } } void PerformDodgeFront() { if (Input.GetKeyDown(KeyCode.E)) { animator.Play("DodgeFrontAnimation"); Vector3 dodgeDiretion = transform.forward * dodgeDistance; characterController.Move(dodgeDiretion); } } public IEnumerator PlayHitDamageAnimation(int damage) { yield return new WaitForSeconds(0.2f); // play a random hit sound if(hitSounds != null && hitSounds.Length > 0) { int randomIndex = Random.Range(0, hitSounds.Length); AudioSource.PlayClipAtPoint(hitSounds[randomIndex], transform.position); } // decrease health currentHealth -= damage; healthBar.SetHealth(currentHealth); if(currentHealth <= 0) { Die(); } animator.Play("HitDamageAnimation"); } void Die() { Debug.Log("Player died."); } public void Attack1Effect() { attack1Effect.Play(); } public void Attack2Effect() { attack2Effect.Play(); } public void Attack3Effect() { attack3Effect.Play(); } public void Attack4Effect() { attack4Effect.Play(); } // Called by UI attack button public void MobileAttack1() => PerformAttack(0); public void MobileAttack2() => PerformAttack(1); public void MobileAttack3() => PerformAttack(2); public void MobileAttack4() => PerformAttack(3); }