I’m trying to set up an “attack” using boxcolliders, I’ve set up so that the collider attached to the player character becomes active when you press a button and is supposed to take health from a object tagged as enemy, but the enemy only takes damage when its moving into the active collider or vice versa, but not when stationary.
these are my scripts:
public class PlayerAttack : MonoBehaviour {
private bool attacking = false;
private float attackTimer;
private float attackCd = 0.3f;
public Collider2D attackTrigger;
void Start () {
//turns off hitbox collider at start
attackTrigger.enabled = false;
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown("k") && !attacking)
{
attacking = true;
attackTimer = attackCd;
//turns on hitbox collider if
attackTrigger.enabled = true;
}
if (attacking)
{
if(attackTimer > 0)
{
//subtracts to countdown the cooldown timer
attackTimer -= Time.deltaTime;
}
else
{
//turns off hitbox collider after attack
attacking = false;
attackTrigger.enabled = false;
}
}
}
}
public class AttackTrigger : MonoBehaviour {
public int dmg = 20;
private void OnTriggerEnter2D(Collider2D col)
{
if (col.isTrigger != true && col.CompareTag("Enemy"))
{
col.SendMessageUpwards("Damage", dmg);
}
}
}
public class EnemyBehavior : MonoBehaviour {
public float currHealth;
// Update is called once per frame
void Update () {
if(currHealth <= 0)
{
Destroy(gameObject);
}
}
public void Damage(int damage)
{
currHealth -= damage;
}
}
EDIT: Here is the movement script
public class PlayerMovement : MonoBehaviour {
public float movementSpeed;
public float jump;
public Rigidbody2D rb;
public bool onGround = true;
public float dblJmp = 0f;
public float dashSpeed;
public float dashTime;
public float dashInc;
public float dashMax;
// Use this for initialization
void Start () {
}
void Update() {
//moves player right
if (Input.GetKey("d") || Input.GetKey(KeyCode.RightArrow))
{
transform.Translate(Vector3.right * Time.deltaTime * movementSpeed);
if (Input.GetKeyDown("j"))
{
rb.AddForce(transform.right * dashSpeed);
rb.velocity = Vector3.zero;
}
}
//moves player left
if (Input.GetKey("a") || Input.GetKey(KeyCode.LeftArrow))
{
transform.Translate(Vector3.left * Time.deltaTime * movementSpeed);
if (Input.GetKeyDown("j"))
{
rb.AddForce((-1 * transform.right) * dashSpeed);
rb.velocity = Vector3.zero;
}
}
//makes player jump and double jump and checks if player is on ground before allowing jump
if ((Input.GetKeyDown("w") || Input.GetKeyDown(KeyCode.UpArrow)) && (onGround == true) /*&& (dblJmp < 2)*/)
{
//sets velocity of jumping player to zero before jump to make jump height consistent
rb.velocity = Vector3.zero;
rb.AddForce(transform.up * jump);
dblJmp++;
}
if (dblJmp < 2)
{
onGround = true;
}
else
{
onGround = false;
}
}
//checks if player is on the ground, and sets double jump counter to zero if on ground
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
dblJmp = 0f;
onGround = true;
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
onGround = false;
}
}
}