Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question ontriggerentry occasionally, consistently seems to fail??

Discussion in 'Physics' started by tsmspace, Aug 22, 2023.

  1. tsmspace

    tsmspace

    Joined:
    Oct 6, 2019
    Posts:
    39
    I have a racing game. THe player flies either a spaceship or lander through trigger meshes. Each racemesh has two accompanying direction-detector meshes, so that the player can assign a given hoop to work in either direction. The ship has a tagged duplicate mesh of the largest part of it's body to be the mesh that triggers gates. It is also possible to dock to a weight and then race with it,, IN order for the gate to know it needs a weight, the player must push the weight around first in builder mode. The weight has a detector mesh bubble that will hit the racemesh before the shipmesh so that the gate can know the weight is attached.

    Here's what's happenening, I have two issues. The first one seems to move around, have inconsistency, and be something I can observe. IF I play in the editor, I can watch the "hasweight" list check a box when I don't have a weight. Sometimes,, the mesh seems to think I have a weight while in buildmode, so that when I go to play in racemode, I can't advance the gate without a weight. It doesn't always happen to the same gate. There appears to be no explanation I can observe. There's a lot I don't know about unity, though.

    the second issue is different,, it doesn't move around, once it happens it never goes away, and it doesn't show up in my lists. Sometimes, I need to go through a gate more than once before it will trigger. If this is the case, it's the same gate(s) every time, and there is no way to make it stop. Once I build a course, if a gate won't register the first pass, it will never register the first pass. IT can happen that if I bonk around, I can set the gate to be twice in a row,, but this is not what is happening here,, my lists just don't advance the first time. It stays on (in this case) 71 until the second pass, always the second pass, no matter how many times I rebuild the course.

    I've tried auto-syncing transforms, turned it back off, maybe there was no effect. I then tried reuse collision callbacks,, well this time I didn't have it happen that a gate needs weight. I don't know if it made a difference though, because that one was already inconsistent, and would move around. I am very confident that on my last builder of the course ,, I didn't do any bonking and spinning out ,,, These gates that won't advance are ones that I passed gently and smoothly through to add next, and similarly while racing flew gently and smoothly through.

    My question ultimately is,,, is this a physics engine issue?? The physics engine should be responsible for detecting collisions,, but in this case I don't know if there's a problem there,, the symptoms dont require that collision detection isn't working,, it could easily be something else,, but I have no idea where to look. ???
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,326
    I don't really understand what your set-up looks like, but here is a chart that explains when OnTriggerEnter Occurs:
    https://docs.unity3d.com/Manual/CollidersOverview.html
    scroll down to where it says:
    Trigger messages are sent upon collision
    whether the object is static is important, also whether it has a rigidbody and whether the rigidbody is kinematic or dynamic can make a difference.

    If a moving object is fast enough to pass all the way through a collider in a single frame, then the collision may not trigger. Setting the the moving object's rigidbody's collision detection to "continuous" might help with that.

    Moving object by directly modifying it's position will bypass the physics engine and so the trigger event might not fire in that case.
     
    tsmspace likes this.
  3. tsmspace

    tsmspace

    Joined:
    Oct 6, 2019
    Posts:
    39

    the racemeshes do not have a rigidbody. The playership/racer AND builder are the same object, and DO have a rigidbody. It's a rare enough occurrence, however, that I don't think this is what is relevent. THe rigidbody is not kinematic for the player.

    the object is not moving fast enough to make it through in one frame, I had this issue before, I think, and fixed it by making the meshes in the hoops thicker and further apart, so that the playership can't possibly move through in one frame at those velocities. Nothing is being moved by altering its transform.

    The system works a lot. I can place 200 rings in a line and not get erroneous behavior ,, but if I make a long course,, maybe 25 rings, and fly around it 4 times,,, then ON THE SAME LAP, but NOT ALL LAPS a ring will not trigger properly. ,,, in the most recent case,, on the 4th lap, 2 rings don't work on the first pass through. I can play it over and again,,, they work the first 3 laps, and on lap 4 they fail 100% of the time. I can rebuild it multiple times and on every rebuild the same 2 rings need 2 passes to trigger.

    If I manually set the counter to those numbers, so that I pass through it without going the course a full 3 times first,, the behavior goes away,, I didn't try passing through the ONE hoop 71 times to see if the 71st time it happens,, but it doesn't appear to correlate to my scripts at all,, it must be some other setup issue??

    edit::: well,, it takes like a half an hour to test it,, I tested it 3 times,, it failed,, then posted that reply,, now without any further changes it passes the test. I can't test it 3 more times just now, I have to go hang raingutters for a living, but very strange. I am still very interested to know more about setting up the colliders , as the documentation is at best non-comprehensive, and at worst non-complete. I feel like if I knew more, I could know where the weaknesses were in my setup (how I've got my meshes,, what physics settings to use, things like that). I am not a programmer,, though,, so for me it's all just trying to figure out based on searches. anyway,, thanks!! I will return of course,, to report if it seems fixed after multiple passes (potentially hours of playtesting) of if it seems to pass intermittently, etc.
     
    Last edited: Aug 22, 2023
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,326
    hmm. I don't know then. Check and see if OnTriggerStay and OnTriggerExit work for each trigger. Maybe you can see where it's breaking down.
     
  5. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,874
    are you sure you are not just moving too fast for the given rigid body collision detection type?

    Try setting to one of the continuous modes and see if that helps : https://docs.unity3d.com/ScriptReference/Rigidbody-collisionDetectionMode.html

    try CollisionDetectionMode.ContinuousDynamic as that is made for fast moving objects that otherwise count as a "miss"

    And then for static objects, set it to https://docs.unity3d.com/ScriptReference/CollisionDetectionMode.Continuous.html

    if you read the docs it says that it needs these to be able to properly have moving things collide well - Good luck! :)