Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Sphere Collider triggering erratically due to unseen oscillation of physics?

Discussion in 'Physics' started by Ziplock9000, May 14, 2019.

  1. Ziplock9000


    Jan 26, 2016
    I have a mob spawning system that uses a very large Sphere Collider tied to a gameObject to denote where a pack of monsters should be spawned on a terrain. The radius is 30m.
    When the player enters the sphere the mobs are spawned. Despawning usus an unrelated system.

    The player has a NavMeshAgent, a rigidbody and sits on a terrain. The following settings are used:

    I'm having this situation where OnTriggerEntered and OnTriggerExited are firing one after another in rapid succession often at random times, which I could understand if I was standing right at the edge of the sphere and due to physics and rounding errors I was extremely subtly going in and out by 0.001 or something.

    However after logging the distance between the player and the colliders center when the event is triggered it's varying by a huge amount, even though I'm standing still!
    It can vary from a distance of 33.8 to 48.5.. Interesting it's the player's y axis that seems to be oscillating from -30.1 to -9.8.

    Is there some physics option or interpolation of the position I've missed?

    I could tweak the values I've shown in the screenshots until it feels better, but I'd rather someone with experience let me know for sure, thanks

    UPDATE 1:
    Just as an FYI, visually there is no player y oscillation at all, it appears he's always on the terrain. Even in the inspector.. but not when I dump the values to the log

    UPDATE 2:
    Although the player's y position when passed through by the two triggers is is vibrating. When I dump the players position directly during Update() in a script that is on it it's rock solid. However they are for sure the same object. I've even checked by dumping out the GetInstanceID() of both of them.
    Last edited: May 15, 2019
  2. dgoyette


    Jul 1, 2016
    I'd probably try to disable some stuff to pinpoint the issue, starting with the navmesh agent. Does this happen if you manually move your player into one of these trigger spheres? It's possible that while navmesh is recomputing a path, it very temporarily gets confused about where your player should be, and warps it somewhere it doesn't belong.

    I'd confirm whether this still happens if you disable navmesh, and just manually move the player around. Then you can decide whether it's a navigation issue or something else.
    Ziplock9000 likes this.
  3. Ziplock9000


    Jan 26, 2016
    I found out what caused the issue, and like so many other times it was simple. It's totally resolved by making sure "isKinematic" is checked.