- Home /
OnTriggerStay2D working only when player ISN'T moving
I've looked for this but I've only seen this question asked in the opposite, namely that OnTriggerStay or OnTriggerEnter is only working when their player IS moving. For my case, for some weird reason, my OnTriggerStay2D only works when my player ISN'T moving.
I'm trying to do knockback when the enemy hits a certain collider thats a little bit ahead of the player and the player is attacking. Both the enemy and the box collider ahead of the player are set to isTrigger. Here's the code in the enemy when he collides:
void OnTriggerStay2D(Collider2D c)
{
if (c.tag == "Player")
{
Player player = (Player)c.transform.parent.GetComponent<Player>();
if (player.attacking)
{
Debug.Log ("Anybody home?");
myRigidbody.AddForce(transform.InverseTransformDirection(c.transform.position - transform.position) * -500);
}
}
}
This code works fine for me in terms of knocking back the enemy, but it only works when the player is standing still.
Any help with this would be greatly appreciated!
I'm bumping this because I seriously can't figure this out. Nothing I can find should make this happen.
I've removed all if statements within the function so theres nothing else that could be causing it. Just straight up, OnTriggerStay won't work unless I'm standing still.
Can you debug and check if the colliders are really touching or that player collider is within the trigger? Also it could be a bug from the new 2D features.
I've checked both: the enemy is definitely hitting the trigger, and I just moved the trigger outside the player's hitbox and its still not firing
Does your trigger have a rigid body? And for some trivial debug, have you set a Debug.log within the body of the OnTriggerStay just to check if its firing?
Answer by belvita · Dec 03, 2013 at 01:32 PM
use void OnTriggerStay only
I'm working with 2D stuff and that function doesnt really work with 2D stuff. I've decided I'm just scrapping that method altogether and using raycasts. They're working more reliably already.
Answer by GamersFrenzy · Jul 15, 2014 at 04:10 AM
Not the script, add Rigidbody to both the Player and the Trigger Collider and check kinematic on both I also set Collision Detection to continuous That should help out.