- Home /
Need help with 2D Attack, attacking in every direction
I'm currently building my first makeshift game by watching tutorials. I have some prior programming knowledge due to my studies. I managed to do movement just fine but I am having problems with creating an Attack Function. Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAttack : MonoBehaviour
{
[SerializeField] public Animator animator;
private Transform attackPoint;
[SerializeField] public Transform normalAttackPoint;
[SerializeField] public Transform lowerAttackPoint;
[SerializeField] public Transform higherAttackPoint;
[SerializeField] public LayerMask enemyLayers;
[SerializeField] public float attackRange = 0.5f;
[SerializeField] public int attackDamage = 40;
void Start()
{
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log("Attack Button Pressed");
if (Input.GetButtonDown("Up"))
{
Debug.Log("Attack Up Succesful");
//animator.SetBool("AttackUp", true)
attackPoint = higherAttackPoint;
}
else if (Input.GetButtonDown("Down"))
{
Debug.Log("Attack Down Succesful");
//animator.SetBool("AttackDown", true)
attackPoint = higherAttackPoint;
}
else {
attackPoint = normalAttackPoint;
}
Debug.Log("Current Attack Point is: " + attackPoint.name);
Attack(attackPoint);
}
}
private void Attack(Transform attackPoint)
{
Debug.Log("Attack Function Initiated");
//animator.SetTrigger("Attack");
Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position,
attackRange, enemyLayers);
foreach (Collider2D enemy in hitEnemies)
{
Debug.Log(enemy.name + " (was hit.)");
enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
}
//animator.SetBool("Down", false)
//animator.SetBool("Up", false)
}
private void OnDrawGizmosSelected()
{
if (normalAttackPoint == null) return;
Gizmos.DrawWireSphere(normalAttackPoint.position, attackRange);
if (lowerAttackPoint == null) return;
Gizmos.DrawWireSphere(lowerAttackPoint.position, attackRange);
if (higherAttackPoint == null) return;
Gizmos.DrawWireSphere(higherAttackPoint.position, attackRange);
}
}
Now for the Enemy Class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
[SerializeField] public int maxHealth = 100;
private int currentHealth;
void Start()
{
currentHealth = maxHealth;
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
if (currentHealth <= 0) Die();
}
private void Die()
{
Debug.Log("Enemy died!");
//Death Animation
//Disable Enemy
}
}
I created 2 Axes in the InputManager called "Up" and "Down" which use "w" and "s" respectively in both negative and positive button fields.
The attackpoints are filled in the Player Inspector with Empty Objects at the right positions.
When Attacking the Logs: "Attack Button Pressed" "Current Attack Point is: Attack Point Normal" "Attack Function Initiated"
The problem is -> i always get "Attack Point Normal" even when pressing "w" for up or "s" for down Can someone tell my why this hapens and how i can fix it?
Apreciate it ifg you took time to look at this mess ^^