Hey, i am trying to make an hitbox when player press a key.
I made a collider that appear for a second and then disappear
I made the collider triggered.
In the enemy script i wrote this:
public void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("PlayerWeapon"))
{
Debug.Log("Do Something");
}
}
It works but not all the time.
I guess its because the hit box does not move everytime and stay still.
But what can i do with it?
I tryed to make a coroutine that move the Collider but that’s not work either.
If you just want to check whether a player is hitting a collider box you could just use Unity - Scripting API: Physics.CheckBox rather than creating a collider and making it visible.
Collider[] hitColliders = Physics.OverlapBox(gameObject.transform.position, transform.localScale / 2, Quaternion.identity, m_LayerMask);
int i = 0;
//Check when there is a new collider coming into contact with the box
while (i < hitColliders.Length)
{
//Output all of the collider names
Debug.Log("Hit : " + hitColliders[i].name + i);
//Increase the number of Colliders in the array
i++;
}