Devlog 2- Enemies, basic and heavy

I have created two enemies that are killable with a melee attack.

Both enemies are using the same script to chase the player. One enemy has a higher health level than the other.

public float moveSpeed = 2f;
public int maxHealth = 30;
public int attackDamage = 10;
private int currentHealth;
public int scoreValue = 10;
private Transform player;
private Rigidbody2D rb;

void Awake()
{
rb = GetComponent();
currentHealth = maxHealth;
}

void Start()
{
GameObject playerObj = GameObject.FindGameObjectWithTag(“Player”);
if (playerObj != null)
{
player = playerObj.transform;
}
}

void FixedUpdate()
{
if (player == null) return;

Vector2 direction = ((Vector2)player.position – rb.position).normalized;
rb.MovePosition(rb.position + direction * moveSpeed * Time.fixedDeltaTime);

}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag(“Player”))
{
Health playerHealth = other.GetComponent();
if (playerHealth != null)
{
playerHealth.TakeDamage(attackDamage);
}
}
}
public void TakeDamage(int amount)
{
currentHealth -= amount;
if (currentHealth <= 0)
{
Die();
}
}

void Die()
{
if (Score.Instance != null)
{
Score.Instance.AddScore(scoreValue);
}
Destroy(gameObject);

}

Scroll to Top