
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D rb;
public Animator animator;
private Vector2 movement;
void Update() { // Get movement input movement.x = Input.GetAxisRaw("Horizontal"); movement.y = Input.GetAxisRaw("Vertical"); // Set animation parameters animator.SetFloat("Horizontal", movement.x); animator.SetFloat("Vertical", movement.y); animator.SetFloat("Speed", movement.sqrMagnitude); } void FixedUpdate() { // Move player rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime); }
}
public class EnemyAI : MonoBehaviour
{
public Transform target;
public float speed = 3f;
public float attackRange = 1.5f;
public int enemyHealth = 100;
void Update() { if (Vector2.Distance(transform.position, target.position) > attackRange) { transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime); } } public void TakeDamage(int damage) { enemyHealth -= damage; if (enemyHealth <= 0) { Destroy(gameObject); } }
}
public class CombatSystem : MonoBehaviour
{
public int playerDamage = 25;
public Transform attackPoint;
public float attackRange = 1f;
public LayerMask enemyLayers;
void Update() { if (Input.GetKeyDown(KeyCode.Space)) { Attack(); } } void Attack() { Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers); foreach (Collider2D enemy in hitEnemies) { enemy.GetComponent<EnemyAI>().TakeDamage(playerDamage); } }
}
public class HealthSystem : MonoBehaviour
{
public int maxHealth = 100;
private int currentHealth;
void Start() { currentHealth = maxHealth; } public void TakeDamage(int damage) { currentHealth -= damage; if (currentHealth <= 0) { Debug.Log("Player Died!"); } }
}
No Responses