- Home /
how to enable collider and after the hit to disable
hey every one i am trying to make a 2D game, i want that every time i press "Space" it will hit with the weaponCollider.
its important that the collider will be disabled at the beginning and only when ill press the space bar it will be on and off after 0.5 sec or 1 sec...
this is what i got on code, its not working good at all, i hope some1 can help me.
thank you, Ben.
[SerializeField]
public EdgeCollider2D WeaponCollider;
-------------------------------------------------------------------------------------
if (Input.GetKeyDown(KeyCode.Space))
{
playerAnimator.Play("AttackAnimation");
playerAnimator.GetComponent<PlayerOneScript>().MeleeAttack();
}
}
public void MeleeAttack()
{
WeaponCollider.enabled = !WeaponCollider.enabled;
}
Answer by G3R1 · Aug 01, 2017 at 02:51 PM
[SerializeField]
public EdgeCollider2D WeaponCollider;
-------------------------------------------------------------------------------------
if (Input.GetKeyDown(KeyCode.Space))
{
playerAnimator.Play("AttackAnimation");
playerAnimator.GetComponent<PlayerOneScript>().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 don't think this will work, this will probably enable the collider, then start the coroutine which will immediately turn the collider off and then wait one second after for no reason. It should first yield return new waitforseconds(1) and then disable the collider and stop itself.
Answer by eazilec · Aug 01, 2017 at 08:02 PM
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;
}
}
}
Your answer
Follow this Question
Related Questions
Please Help!!! Enable/Disable Script!!! 3 Answers
disabling a script (MouseLook) 5 Answers
Prefab is lost in between 1 Answer
Disalbe/Activate script if a key is pressed. 1 Answer
how to disable a script without setting script.enabled? 2 Answers