Dash through enemies and dont taking damage
I'm trying to make my Player character dash through specific enemies and don't take any damage during de dash animation, here is the script:
using UnityEngine; using System.Collections; using CnControls;
public class PlayerDash : MonoBehaviour {
private Animator myAnim;
private bool dash = false;
private float dashTimer = 0;
private float dashCd = 0.3f;
public Collider2D[] Coll;
void Start () {
myAnim = gameObject.GetComponent<Animator> ();
}
void Update () {
if (CnInputManager.GetButtonDown ("Dash") && !dash) {
dash = true;
dashTimer = dashCd;
myAnim.SetBool ("Dash", dash);
foreach (Collider2D coll in Coll) {
if (coll.gameObject.tag == ("Enemy")) {
coll.enabled = false;
}
}
}
if (dash) {
if (dashTimer > 0) {
dashTimer -= Time.deltaTime;
} else {
dash = false;
myAnim.SetBool ("Dash", false);
}
}
}
}
Everything is working fine, but when i use dash and collide with a "dasheable" enemy, every "dasheable" enemy don't inflict damage to the Player(that's because the condition i use), what conditions should i use to make enemy colliders still being active after i dash the first one, already tryed some options here and nothing seems to work properly
Answer by DanielBrg · Jun 15, 2016 at 10:27 PM
Figured out, i created a method outside Update to check if the Player are using dash and then i called it back in Update, thanks.
void enterDashing() {
foreach (Collider2D coll in Coll) {
if (coll.gameObject.tag == "Enemy" && !dash) {
coll.enabled = true;
}
}
}
Answer by jgodfrey · Jun 15, 2016 at 10:15 PM
If I understand what you're after, I think you just want to re-enable the colliders when the dash timer runs out, right? If that's the case, when you reset "dash = false", you'd also want to repeat your "foreach (Collider2D ..." loop again, but this time reset the coll.enabled = true.
So, something like:
} else {
dash = false;
myAnim.SetBool("Dash", false);
foreach (Collider2D coll in Coll) {
if (coll.gameObject.tag == ("Enemy")) {
coll.enabled = true;
}
}
}
Though, instead of repeating that code, you could refactor it into its own method and pass in an argument specifying whether you want to enable or disable the colliders. Then, simply call it from both locations with an appropriate true or false.
Answer by LeVeN_47 · Mar 22, 2021 at 06:55 AM
we can use co-routines and turn off boxcoliider or boxcollider2D component of Enemy gameobject while animation is going on for few seconds to avoid damaging the player.