using UnityEngine; namespace IndianOceanAssets.Engine2_5D { public class SwordAttack : MonoBehaviour { // Damage dealt by the sword attack [Range(10, 40)] [SerializeField] private int damageAmount; // Radius around the attack position where enemies can be hit [Range(0.1f, 5.0f)] [SerializeField] private float slashRadius; // Particle effect played during slash [SerializeField] private ParticleSystem swordSlashParticle; // Transform that defines the attack position/origin [SerializeField] private Transform attackPos; // Sound effect played on slash [SerializeField] private AudioSource swordSlashSFX; // Layers to detect enemies and plantation objects [SerializeField] private LayerMask whatIsEnemy; [SerializeField] private LayerMask whatIsPlantation; // Key assigned to trigger the attack [SerializeField] private KeyCode attackKeyCode; private void Update() { // Flip player sprite based on horizontal input direction Vector2 input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")); if (input.x > 0) transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z); else if (input.x < 0) transform.localScale = new Vector3(-Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z); // Trigger slash when attack key is pressed if (Input.GetKeyDown(attackKeyCode)) { Slash(); } } // Executes the sword slash logic public void Slash() { // Play particle and sound effects if (swordSlashParticle != null) swordSlashParticle.Play(); if (swordSlashSFX != null) swordSlashSFX.Play(); // Detect and damage enemies within the slash radius Collider[] enemies = Physics.OverlapSphere(attackPos.position, slashRadius, whatIsEnemy); if (enemies != null) { foreach (var E in enemies) { HealthSystem health = E.GetComponent(); if (health != null) { health.Damage(damageAmount); } } } // Detect and cut plantation objects (like bushes) within radius Collider[] plantation = Physics.OverlapSphere(attackPos.position, slashRadius, whatIsPlantation); if (plantation != null) { foreach (var P in plantation) { Plantation plant = P.GetComponent(); if (plant != null) { plant.Cut(); } } } } // Draws the slash radius in the editor for visualization private void OnDrawGizmosSelected() { if (attackPos != null) { Gizmos.color = Color.red; Gizmos.DrawWireSphere(attackPos.position, slashRadius); } } } }