- Home /
multiple objects on oncollissionstay2d
I have a Kinemetaic floor rigidbody attached with oncollisionstay2d i want to perform some action on all enemeies whihc is on collisionstay with floor, But The action will be performed to one enemy on each hit. I want if there is 5 enemies on floor, to perform condition inside the if condition to be performed for all enimes whihc was onstay with floor
void OnCollisionStay2D(Collision2D coll)
{
if (coll.gameObject.tag == "Enemy")
{
if (hit && once)
{
GameObject enemy = coll.gameObject;
Enemy enimtp = (Enemy)enemy.GetComponent("Enemy");
var animctr = enimtp.GetComponent<Animator>();
if (enimtp.HP == 2)
{
--enimtp.HP;
animctr.SetTrigger("Hit");
}
else if (enimtp.HP == 1)
{
animctr.SetTrigger("Awake");
++enimtp.HP;
}
once = false;
}
}
}
OnCollisionStay () does get called once for each object that remains in contact with the collider during this frame.
If you're not getting the behaviour you expected, I'd check your hit
and once
variables, which seem to be modifying the behaviour.
The Variable Hit is just a trigger, when the action need to be performed during oncollision stay variable Once, i am using to perform the action only once, otherwise it wil perform same action many times
And where do you declare those variables? You haven't made them static
, have you?
HI Tanoshimi, Thanks a lot for your great assistance. Actually, what i want to achieve is, when player touches the floor, i want to perform some actions on all enemies which was staying on top of floor. I added variable once because, i wanted to perform this action only once when player touches the floor from underneath of floor, when player just touches and comes back, i want to perfrom some action on all enemies which was staying on top of floor at that time. Thanks you once again Tanoshimi