- Home /
OnTriggerStay2D Broken
HEY! I have a problem in my game. so i wanted to make a lever in the game that would activate something or trigger and animation. and it worked BUT, I Used the onTriggerStay2D Function to know if i am colliding with the lever and if i am and i press a button. it triggers the thing or activates an object. and it randomly stops working for some reason. its just like they weren't colliding. and then they randomly work again. i searched and tried to fix it. (BTW I tried making the Rigidbody Never sleep) and nothing worked. i need help
here is the really bad code if u want to look at it
private void OnTriggerStay2D(Collider2D collision)
{
//Activating / Deactivating objects
if (activateSomeThing && objectActivated == false && Input.GetKeyDown(KeyCode.E) && collision.CompareTag("Player"))
{
objectToActivate.SetActive(true);
objectActivated = true;
leverAnimator.SetBool("Open", true);
}
else if (activateSomeThing && objectActivated == true && Input.GetKeyDown(KeyCode.E) && collision.CompareTag("Player"))
{
objectToActivate.SetActive(false);
objectActivated = false;
leverAnimator.SetBool("Open", false);
}
//Toggling Animations
if (toggleAnimations && animationToggled == false && Input.GetKeyDown(KeyCode.E) && collision.CompareTag("Player") && _bool)
{
animatorToToggle.SetBool(boolOrTriggerName, true);
animationToggled = true;
leverAnimator.SetBool("Open", true);
}
else if (toggleAnimations && animationToggled == true && Input.GetKeyDown(KeyCode.E) && collision.CompareTag("Player") && _bool)
{
animatorToToggle.SetBool(boolOrTriggerName, false);
animationToggled = false;
leverAnimator.SetBool("Open", false);
}
if (toggleAnimations && animationToggled == false && Input.GetKeyDown(KeyCode.E) && collision.CompareTag("Player") && trigger)
{
animatorToToggle.SetTrigger(boolOrTriggerName);
animationToggled = true;
leverAnimator.SetBool("Open", true);
}
else if (toggleAnimations && animationToggled == true && Input.GetKeyDown(KeyCode.E) && collision.CompareTag("Player") && trigger)
{
animatorToToggle.SetTrigger(boolOrTriggerName);
animationToggled = false;
leverAnimator.SetBool("Open", false);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
//Activating / Deactivating objects
if (activateSomeThing && objectActivated == false && Input.GetKeyDown(KeyCode.E) && collision.CompareTag("Player"))
{
objectToActivate.SetActive(true);
objectActivated = true;
leverAnimator.SetBool("Open", true);
}
else if (activateSomeThing && objectActivated == true && Input.GetKeyDown(KeyCode.E) && collision.CompareTag("Player"))
{
objectToActivate.SetActive(false);
objectActivated = false;
leverAnimator.SetBool("Open", false);
}
//Toggling Animations
if (toggleAnimations && animationToggled == false && Input.GetKeyDown(KeyCode.E) && collision.CompareTag("Player") && _bool)
{
animatorToToggle.SetBool(boolOrTriggerName, true);
animationToggled = true;
leverAnimator.SetBool("Open", true);
}
else if (toggleAnimations && animationToggled == true && Input.GetKeyDown(KeyCode.E) && collision.CompareTag("Player") && _bool)
{
animatorToToggle.SetBool(boolOrTriggerName, false);
animationToggled = false;
leverAnimator.SetBool("Open", false);
}
if (toggleAnimations && animationToggled == false && Input.GetKeyDown(KeyCode.E) && collision.CompareTag("Player") && trigger)
{
animatorToToggle.SetTrigger(boolOrTriggerName);
animationToggled = true;
leverAnimator.SetBool("Open", true);
}
else if (toggleAnimations && animationToggled == true && Input.GetKeyDown(KeyCode.E) && collision.CompareTag("Player") && trigger)
{
animatorToToggle.SetTrigger(boolOrTriggerName);
animationToggled = false;
leverAnimator.SetBool("Open", false);
}
}
}
I don't understand why you would write code like this. You don't need to repeat yourself so many times when you write Input.GetKeyDown(KeyCode.E) && collision.CompareTag("Player")
as a conditional. Anyways, if you put a Debug.Log in there, does it ever not print or leave intervals between printing? Try this first and let me know the results:
void OnTriggerStay2D(Collider2D collision)
{
Debug.Log(collision.gameObject.name);
}
Answer by EvanCheddar · Feb 04, 2021 at 10:54 PM
This code blows my mind. I don't understand why you'd need this much spaghetti code to do something seemingly very simple. Seems like you want to trigger something to happen with an animation on a specific collision. If you explain what you want to happen more clearly, I can try to help you, otherwise I don't think you'll have much luck on here.
i want to triiger something(An animation(A bool or a trigger) or activate an object. and thats why there is alot of spaghetti code) so i want to trigger that thing or animation when i am close to the object and when i press $$anonymous$$ so i made a trigger collider around the lever that will trigger the object or animation and made an ontrigger function. so when i enter the collider. the ontrigger function will work. so i will know that i am in the range of the lever. so if i press E and i am colliding with the trigger boxcollider around the lever. i can use the lever and do everything(and thats also why this code is so long. because i made it so it can activate a trigger animation. or a bool animtaion. or activate an object)
Answer by Osinskis · Feb 05, 2021 at 10:14 PM
When a marble collides with the two side walls I use OnTriggerEnter2D() and rotate the angle so it bounces off the walls at an angle. When the marble collides with the top wall it basically freezes, at which point I remove the rigidbody component and set a "topObjects" tag