Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Jul 22, 2017 at 05:08 PM by merkaba48 for the following reason:

Have determined it is a fault with the OnTrigger methods, and resorted to checking collisions within the Update() method instead.

avatar image
0
Question by merkaba48 · Jul 22, 2017 at 03:03 PM · triggerbug-perhapscollision2d

trigger inconsistent behaviour?

alt text

Hi, I have a strange problem. My character 2D collision sometimes does not fire the OnTriggerEnter2D method when entering a trigger collision for 1 frame. This happens about 10% of the time, when performing the exact same movement each time.

My supplied image shows the position of the character's collision box for each frame, and you can see where it is overlapping the trapezoid trigger.

Some more info: - There is nowhere in my script where I disable the collision on the character - The world positions of the character at each step are replicated exactly each time the jump is performed. - A new coroutine is run every frame to draw the position at that frame for 2 seconds - Rather than move my character with Time.deltaTime, I use a fixed value (1/60) to ensure consistent positions from identical movement.

As far as I can tell, sometimes it just "doesn't work".

Any help would be appreciated! Here's my ontrigger method (very simple),

 void OnTriggerEnter2D(Collider2D collider)
     {
         Debug.Log("Hit trigger");
     }


(Update)

So I tried having my character simply run into a trigger. Most of the time it correctly 'dies' when entering the trigger, but now and again it will be inside the trigger for three extra frames before detecting that it is inside the triggger. What gives? My Physics2D values are all default BTW.

Left image shows the character 'dying' after being inside the red trigger for 1 frame, right image shows the delayed trigger. (blue boxes are just death particles) alt text

(Update2)

This is the coroutine that runs every frame, drawing the position during that frame for 2 seconds,

 IEnumerator DebugTemporarilyDrawPosition()
     {
         float t = 2f;
         Color c = DebugDrawColor;
         Vector3 position = transform.position;
         while (t > 0)
         {
             float xo = _baseScale.x * 0.5f;
             float yo = _baseScale.y * 0.5f;
             Debug.DrawLine(new Vector3(position.x - xo, position.y - yo, 0), new Vector3(position.x - xo, position.y + yo, 0), c);
             Debug.DrawLine(new Vector3(position.x + xo, position.y - yo, 0), new Vector3(position.x + xo, position.y + yo, 0), c);
             Debug.DrawLine(new Vector3(position.x - xo, position.y - yo, 0), new Vector3(position.x + xo, position.y - yo, 0), c);
             Debug.DrawLine(new Vector3(position.x - xo, position.y + yo, 0), new Vector3(position.x + xo, position.y + yo, 0), c);
             yield return null;
             t -= FixedTime.delta;
         }
     }

nocollision.png (12.3 kB)
no-collision2.jpg (50.7 kB)
Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
0

Answer by Vollmondum · Jul 22, 2017 at 03:23 PM

Add a bool and move your code to OnTriggerStay (if false, make it true and execute collision). Trigger enter fails sometimes as you noticed

Comment
Add comment · Show 2 · 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 merkaba48 · Jul 22, 2017 at 03:34 PM 0
Share

so I already have an '_alive' bool that gets set to false when entering the trigger, and bails out of the trigger method if not alive, so I simply changed it from OnTriggerEnter2D to OnTriggerStay2D - and the results are still inconsistent as well as taking 4 frames longer $$anonymous$$imum (so 4 $$anonymous$$imum detection time, 7 frames maximum), so unfortunately this solution actually gives worse results.

Perhaps the only way I can ensure reliable results is to scrap using OnTrigger methods entirely and put in an overlaps test into the Update() method, which is a shame as I'd have expected OnTrigger to do its job.

I appreciate your help besides

avatar image Vollmondum merkaba48 · Jul 22, 2017 at 06:51 PM 0
Share

Why don't you create 3D colliders ins$$anonymous$$d? In fact, since OnStay is taking 4 frames longer, you might wanna check if some method is being continuously called, that might be a cause for the delay. Use "print" statement to check if your main lines are only executed once per necessity. I mean people overestimate Update function hassling it all the time. The only thing that should really be constantly Updated are movement and time-dependent counters, like countdown.

Follow this Question

Answers Answers and Comments

84 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

Related Questions

2D Triggers and Collisions not working as expected. 2 Answers

OnTriggerEnter only sent to one of the colliding objects, not both? 0 Answers

Trigger detection failing when laggy 0 Answers

Issues with Animator Controller - Trigger does not reset to false after being set with SetTrigger 2 Answers

Neither collision nor trigger being detected 3 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