I’m working on my first platformer game with two characters: a warrior and a mage. I’ve got an enemy with a few box colliders to check if the player is close. The problem is, when my warrior hits the enemy or my mage shoots at it, the hits aren’t detected they seem to be caught in some of the enemy child colliders. But if I attack a square that has the enemy’s health script, the square loses HP. i got close to the enemy so it’s not a range problem.
Any ideas on how to bypasse the child colliders?
Heres the warriors combat script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class combate : MonoBehaviour
{
private Animator animacao; // Declare Animator variable
private float TimeBtwAttack;
public float startTimeBtwAttack;
public Transform attackPos;
public float attackRange;
public LayerMask isEnemy;
public int damage;
public int posture;
private float coolDownTime = 5f; // Cool down time in seconds
private float lastAirslashTime;
public GameObject AirslashPrefab;
private Core Core;
public int AirslashSpeed = 20;
public Transform proSpawnPoint;
void Start()
{
Core=GetComponent<Core>();
TimeBtwAttack = startTimeBtwAttack;
animacao = GetComponent<Animator>(); // Initialize animacao with the Animator component of the GameObject
}
void Update()
{
PreAirslash();
combater();
Block();
}
public void PreAirslash()
{
if (Input.GetKeyDown(KeyCode.Alpha1) && Time.time >= lastAirslashTime + coolDownTime)
{
animacao.SetTrigger("Airslash");
Debug.Log("Airslash GO!!!");
lastAirslashTime = Time.time; // Update the last Airslash time
Airslash();
}
}
public void combater()
{
if (TimeBtwAttack <= 0)
{
if (Input.GetKey(KeyCode.Mouse0))
{
animacao.SetTrigger("Attack");
Collider2D[] enemiestoDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, isEnemy);
for (int i = 0; i < enemiestoDamage.Length; i++)
{
enemiestoDamage[i].GetComponent<InimigoVida>().takeDamage((int)(damage * 1.5));
}
}
TimeBtwAttack = startTimeBtwAttack;
}
else
{
TimeBtwAttack -= Time.deltaTime;
}
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(attackPos.position, attackRange);
}
public void Block()
{
if (Input.GetKeyDown(KeyCode.F)) // Holding "F" key to block
{
Debug.Log("a dar block");
Core playerCore = GetComponent<Core>();
playerCore.takeDamage(0);
playerCore.posture += 25;
}
else if (Input.GetKeyUp(KeyCode.F)) // Release "F" key to stop blocking
{
Debug.Log("paraste de dar block");
Core playerCore = GetComponent<Core>();
if (playerCore.posture >= 100)
{
playerCore.takeDamage((int)(playerCore.MaxHealth * 0.1f));
playerCore.posture = 0;
}
}
}
void Airslash()
{
Vector2 AirslashDirection = Core.isFacingRight ? Vector2.right : Vector2.left;
var fireball = Instantiate(AirslashPrefab, proSpawnPoint.position, proSpawnPoint.rotation);
fireball.transform.localScale = new Vector3(Core.isFacingRight ? 2 : -2, 2, 2);
fireball.GetComponent<Rigidbody2D>().velocity = AirslashDirection * AirslashSpeed;
Collider2D[] enemiestoDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, isEnemy);
for (int i = 0; i < enemiestoDamage.Length; i++)
{
enemiestoDamage[i].GetComponent<InimigoVida>().takeDamage(damage);
}
}
}
and heres the code for my enemy health script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InimigoVida : MonoBehaviour
{
public int maxHealth;
public int health;
// Start is called before the first frame update
void Start()
{
health = maxHealth;
}
// Update is called once per frame
void Update()
{
if (health <= 0)
{
Destroy(gameObject);
}
}
public void takeDamage(int damage)
{
health -= damage;
Debug.Log("took damage");
}
}
if you need more clarification on the problem please say since im not the best a writting phrases and i fear i might say something wrong with the problem