Nein! Tools
Unity Tool script.
Main Features of EnemyController Script Enemy Movement and AI Behavior: Wandering: The enemy can wander within a specified radius (wanderRadius) around its spawn position. It chooses a new random target Read more
Unity Tool script.
Main Features of EnemyController Script
Enemy Movement and AI Behavior:
Wandering: The enemy can wander within a specified radius (wanderRadius) around its spawn position. It chooses a new random target position every wanderInterval seconds and moves towards it at wanderSpeed.
Method: Wander() and ChooseNewWanderTarget().
Chasing: If the player (tagged as "KnightMesh") is within the detectionRange, the enemy will chase the player at an increased speed (wanderSpeed * chaseSpeedMultiplier). Method: MoveTowardsTarget().
Attacking: If the player is within the attackRange and the attack cooldown (attackCooldown) has expired, the enemy will attack the player. Method: Attack().
Health and Damage System: Health Management: The enemy has a health value (health) and a maximum health (maxHealth), which can be set in the Inspector (default 10).
Taking Damage: The enemy can take damage via the TakeDamage method, which reduces its health based on the incoming damage minus armor mitigation (armor * 0.5f). Method: TakeDamage(int damage).
Death: When health reaches 0, the enemy dies, playing a death animation (via the "Die" trigger) and destroying itself after 2 seconds. Method: Die().
Health Bar Display: The enemy has a health bar displayed above it, instantiated from a healthBarCanvasPrefab (a UI Canvas with a Slider).
The health bar’s position is offset by healthBarOffset (default 5 units above the enemy) and updates dynamically as the enemy takes damage.
The health bar is destroyed when the enemy dies.
Animation Control: The script interfaces with an Animator component to control enemy animations: Idle/Walk: Controlled by a Speed parameter (set to 1 for walking, 0 for idle/attacking).
Attack: Triggered by an "Attack" trigger when the enemy attacks.
Hit: Triggered by a "Hit" trigger when the enemy takes damage.
Death: Triggered by a "Die" trigger when the enemy dies, with an IsDead boolean set to true.
Methods: Attack(), TakeDamage(), Die(), Wander(), MoveTowardsTarget().
Damage Text Display: When the enemy takes damage, floating damage text is displayed above it, instantiated from a damageTextPrefab.
The text shows the final damage dealt (after armor mitigation) in red with a font size of 24.
The text position is offset by textOffset (default (0, 1, 0)).
Attack Mechanics: The enemy can attack the player when in range, dealing attackDamage (default 1).
Attacks have a critical hit chance (critChance, default 0.1) and a critical hit multiplier (critMultiplier, default 1.5).
When attacking, the enemy triggers an attack animation and applies damage to the player via a KnightHealth component.
Damage text is displayed above the player when hit, with critical hits shown in yellow and larger font size (30 vs. 24).
Rotation and Facing: The enemy rotates to face its target (either the wander target or the player) using Quaternion.Slerp with a rotationSpeed.
An optional rotationOffsetY can be applied to adjust the facing direction.
Debug and Testing Features: The script includes extensive debug logs to track enemy behavior (e.g., distance to the player, state changes, animation states).
Pressing the T key applies 10 damage to the enemy for testing purposes (you can remove this after testing).
Summary of Key Functionality AI Behavior: Wandering, chasing, and attacking the player based on proximity and cooldowns.
Health System: Tracks health, applies damage with armor mitigation, and handles death.
Health Bar: Displays a UI health bar above the enemy that updates with damage.
Animations: Controls idle, walk, attack, hit, and death animations via an Animator.
Damage Text: Shows floating damage text for both enemy and player damage.
Attack Mechanics: Includes critical hits and applies damage to the player.
Rotation: Smoothly rotates the enemy to face its target.
















