- Home /
onTriggerEnter only fires once if player doesn't move
So I'm building an 2D metroidvania-like action game.
I have a player, with:
rigidbody2d
a polygon collider, non-trigger
a box collider, is trigger, enabled = false, this one is the attack collider
And when I hit attack key, I set enabled to true for like 0.5 seconds, and then set it back to false.
I kept an eye on the check box besides the collider, and I'm sure this set enabled true and false is working properly.
Then I create another target object, with:
a polygon collider, non-trigger
When the target object has onTriggerEnter2D fires, I log it.
So ideally I will make some damage on the target when the onTriggerEnter2D fires.
However, if I let the player stand still and hit the target multiple times, onTriggerEnter2D will fire only once, I have to move my player a little bit, and hit target, to fire the function again.
I also logged it when onTriggerExit2D fires, so I am sure it exits. it just doesn't enter anymore.
Another thing is, if I attack fast and hit the target continuously, the function does fire as many times as I hit it.
but if i stop a while (like 1 second?), and I hit it again, function doesn't fire. i still have to make the player walk a little bit to fire the function again.
this situation happens only when:
( all collider on the target object are non-trigger ) OR ( target has rigidbody2D attached )
If I remove rigidbody2D, and add one isTrigger collider on the target, this thing will not happen.
Anyone can help me with it? I have no idea what causes this.
Thanks!
Answer by tormentoarmagedoom · Feb 20, 2019 at 04:02 PM
Good day.
I think you are confused with OnTriggerEnter and OnTriggerStay. AS its name says, OnTriggerEnter, is executed when enters, so only once. OnTriggerStay, is executed every frame that the collider is inside.
GoodBye!
Hello! so for each attack, I enabled the trigger collider for 0.5 second and then disabled it. therefore for multiple attacks, it should fire onTriggerEnter multiple times, right?
And i'm counting on the onTriggerEnter to calculate the damage, therefore I'm not using onTriggerStay here.
public class DamageDealer : $$anonymous$$onoBehaviour
{
float timer = 0f;
float timeBetweenAttacks = .5f;
private void Update()
{
timer += Time.deltaTime;
}
void OnTriggerStay(Collider other)
{
if (timer >= timeBetweenAttacks)
{
Attack()
}
}
void Attack()
{
timer = 0f;
}
}
Hello JRtheUnity,
If you would like to limit the amount of times you can attack when an object is inside a trigger, you could use a timer! This is quite similar to how the player shoots from the Survival Shooter tutorial.
To explain myself, what I am doing is setting up a timer which equals zero and continues to increase as the game runs. If another collider is found present in my Trigger, the OnTriggerStay function will be called, which calls once every frame. But that would not be good because then the object would take damage once a frame. So, the next portion of my script says, if the timer (which is updated once a frame to have the time it took to go from the last frame to the current frame added to whatever timer currently is) is greater than or equal to the timeBetweenAttacks variable (in this case, .5), call the Attack() Function. Inside the Attack Function, write some code. I would recommend writing if (Input.GetButtonDown("ButtonName")) and then however method you decide to deal damage. At the end of your Attack Function (inside of your if Input statement), make sure to set the timer back to zero, so it now has to wait another .5 seconds before the OnTriggerStay function can call attack again. Let me know if this helps!
Hello dcw2001, Thank you for sharing the code and the thoughts! however, i think it's my bad that I didn't make my question clear. I will update the question later.
Your answer