- Home /
Have damage done to player via colliders, only a certain amount of times?
I'm making a game in Unity, and I've written up a script that whenever the player is hit damage is done. However, there's only one issue, the speed at which the enemy attacks causes a damage hit to be registered 4 times, due to the speed at which it passes through the player's collider. since the enemy only swings two hands at the player in an attack, I only want the damage to register twice.
In summary of problem, both hands swinging at the player in animation, move slow enough through the players collider as they pass, to have unity register collisions twice in each hand, as they pass through the player's collider. Below is the simple script I wrote for damage detection:
private void OnTriggerEnter(Collider other){
if (other.transform.tag == "Player") { // Pro gets the "Player" tag
print("Player hit");
other.transform.gameObject.GetComponent<Health>().TakeDamage(damageAmount);
}
}
In what way could I cap the maximum amount of collision detections, per attack animation, to 2?
Answer by shadowpuppet · May 17, 2018 at 07:46 PM
maybe try OnTriggerExit. Or add an int called "hit" and make that a condition
public int hit;
private void OnTriggerEnter(Collider other){
if(other.CompareTag ("Player")){
if (hit < 2){
hit+=1;
other.transform.gameObject.GetComponent<Health>().TakeDamage(damageAmount);
}
}
}
Your answer
Follow this Question
Related Questions
How to make door open and close with Input.GetButtonDown? 1 Answer
Animations interupting Aim-Script 0 Answers
How would I set up this blend tree for my 2d game 0 Answers
Have some minor problems. Cant tell if its a script issue or a animator issue. 1 Answer
Issue trying to animate multiple models with same controller and script 0 Answers