[SerializeField]
public EdgeCollider2D WeaponCollider;
-------------------------------------------------------------------------------------
if (Input.GetKeyDown(KeyCode.Space))
{
playerAnimator.Play(“AttackAnimation”);
playerAnimator.GetComponent().MeleeAttack();
}
}
public void MeleeAttack()
{
WeaponCollider.enabled = true;
StartCoroutine(DisableWeaponCollider());
}
private IEnumerator DisableWeaponCollider()
{
WeaponCollider.enabled = false;
yield return WaitForSeconds(1.0f);
}
Did't understand what you needed exactly but I think this will help you disable the collider after 1 second.
I’m working on a game that requires something similar and this is basically how I’ve done it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class answer : MonoBehaviour {
public Animator playerAnimator;
public EdgeCollider2D weaponCollider;
//This is true while collider is ENABLED and player is attacking.
//We'll check for this when checking for input to make sure player isn't already attacking.
private bool attacking;
//This is the duration of the attack and how long the collider will be ENABLED for.
//I set its default value to 0.5, but it can be 1 or anything else.
public float attackDuration = 0.5f;
//This is the exact time in seconds since the game started that the attack will end and the collider will be DISABLED.
//We'll calculate this by adding the "attackDuration" to the exact game time in seconds when the attack began.
private float timeToAttackEnd;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//Check for input and if player is attacking.
if(Input.GetKeyDown(KeyCode.Space) && !attacking)
{
playerAnimator.Play("AttackAnimation");
attacking = true;
//Set the exact time that attack will end to be the TIME the attack is called PLUS the DURATION of the attack we specified earlier.
timeToAttackEnd = Time.time + attackDuration;
}
//Check for attacking
if(attacking)
{
//This will be called every frame while attacking is true so we can check that the TIME to end the attack has been reached.
MeleeAttack();
}
}
public void MeleeAttack()
{
//If the TIME to end the attack has been reached, DISABLE the collider
//and set "attacking" to false so this function will no longer be called and we can attack again if we choose.
if(Time.time > timeToAttackEnd)
{
weaponCollider.enabled = false;
attacking = false;
}
//every frame BEFORE the TIME to end the attack is reached, the collider will be ENABLED.
else
{
weaponCollider.enabled = true;
}
}
}