Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by SaguG · Feb 03, 2021 at 11:01 PM · 2d2d gameissueontriggerstaynotworking

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);
     }
 }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Llama_w_2Ls · Feb 04, 2021 at 07:24 AM 0
Share

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);
 }


@SaguG

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SaguG · Feb 14, 2021 at 11:10 PM 0
Share

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)

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

281 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Issue with flipping an enemy when it reaches an edge 1 Answer

Help Me! I want to make a pixel fairy! 1 Answer

Random spawn outside viewport - Wrap around screen 2 Answers

How to make layers become visible with a button prompt?,How to make entire layers active/inactive with a button press 0 Answers

Snake game doesn't respond to input 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges