Fusion and collisions - OnTriggerEnter and OnTriggerExit questions

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on Fusion.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Fusion and collisions - OnTriggerEnter and OnTriggerExit questions

fsGs
2022-06-29 15:24:27

Good afternoon everyone,

Here is what I have:

  • Simple player prefab

  • Player behavior in the prefab. Playerbehaviour is a networkbehavior and has OnTriggerEnter / Exit

  • Simple object with a trigger

Here is what I am experiencing:

  • When the game mode is Gamemode.Host things behave as I expect, OnTriggerEnter/Exit get called correctly and overall all is good

  • When the game mode is Gamemode.Client things go a bit crazy, for example, when the player is on top of the trigger object OnTriggerEnter is called every frame (behaves like OnTriggerStay) and OnTriggerExit is never called. It also behaves incorrectly once the interaction input is used, but I don't want to assume that it is because of this.

Because I am still new to photon fusion I checked some of the samples and this is what i encountered:

On Fusion Karts:

  • The kartEntity (that is a networkbehavior) uses only OnTriggerStay to interact with the powerups via ICollidable interface

  • The RespawnTrigger (that is a monobehavior) has a OnTriggerEnter

On Fusion Razon Madness:

  • The player behavior (that is a networkbehavior) has a custom collision function inside FixedUpdateNetwork() that uses Runner.GetPhysicsScene2D().OverlapBox

So my questions are:

  • What is the correct approach/set up that I need to have in order to have OnTriggerEnter/Exit working correctly in clients? And if that is not possible,

  • Is the correct approach to collisions either using exclusively OnTriggerStay or some sort of custom collision detection that runs on FixedUpdateNetwork?

I am fairly experienced with Unity but new to Photon/Fusion so I can assume that the scene/gameobject setup is correct, so I would love to hear some insight from the Fusion side.

Thanks and best regards!

Comments

Isaac_Augusto
2022-06-30 10:09:34

Hi fsGs,

Yes, as you noticed, we don't recommend using OnTriggerEnter/Exit as they aren't reliable on clients, only on the server. The reason is that on server the simulation is only forward, but on clients it isn't - So it would be OK to use on the server only or a local logic as I believe the RespawnTrigger of the Fusion Karts is.

OnTriggerStay is fine because it don't need the previous state of the collider, only the current one.

Collision detection on FUN is also valid because it will be tick align.

If i'm not mistaken, you can reproduce a similar behavior as Enter/Exit with OnTriggerStay + Networked property + OnChange

I hope that helps to clear some of your questions


Isaac Augusto

Photon Fusion Team

Chazni
2022-06-30 10:19:02

[EDIT] I posted this at the same time as the first reply. I'll leave it here just in case but feel free to ignore it.

=========================

This isn't a solution, just a "me too" comment.

I am getting the same problem, but my OnTriggerExit is also getting called. The trigger causing this is not a network object because it is only being used locally by the client so it can efficiently keep track of which objects the server would let it interact with, while the interactable objects are NetworkRigidBodies with the other trigger attached to them.

It seems to be something to do with how the physics is stepped on the client. It must do something to the physics state that moves the rigid body away then back to where it should be during resimulation. This wouldn't be an issue but the trigger isn't active when the code that checks my inputs is run so the input code think's there's nothing interesting going on and does nothing.

Everything works fine on the host for me too. It's possible that you aren't getting OnTriggerExit due to the trigger being large enough to not move far enough to trigger the exit, or the simulation is not moving the object far enough, assuming it is the same issue I'm having.

This is a callstack of a Debug.Log() in my OnTriggerEnter. OnTriggerExit is exactly the same and is called exactly the same number of times, so the enter/exit is symmetrical.

============================================================

Project.InteractionSource:OnTriggerEnter (UnityEngine.Collider) (at Assets/Scripts/Interaction/InteractionSource.cs:85)

UnityEngine.Physics:Simulate (single)

Fusion.NetworkPhysicsSimulation3D:Simulate () (at Fusion/Fusion.Runtime/Components/NetworkPhysicsSimulation3D.cs:44)

Fusion.NetworkPhysicsSimulation3D:FixedUpdateNetwork () (at Fusion/Fusion.Runtime/Components/NetworkPhysicsSimulation3D.cs:28)

Fusion.SimulationBehaviourUpdater:InvokeFixedUpdateNetwork (Fusion.SimulationStages,Fusion.SimulationModes) (at Fusion/Fusion.Runtime/Components/SimulationBehaviourUpdater.cs:161)

Fusion.NetworkRunner:Fusion.Simulation.ICallbacks.OnTick () (at Fusion/Fusion.Runtime/Runner/NetworkRunner.SimulationCallbacks.cs:396)

Fusion.Simulation:InvokeTick (Fusion.SimulationStages,bool) (at Fusion/Fusion.Runtime/Simulation/Simulation.cs:1270)

Fusion.Simulation:StepSimulation (Fusion.SimulationStages,bool,bool,bool) (at Fusion/Fusion.Runtime/Simulation/Simulation.cs:485)

Fusion.Simulation:Update (double) (at Fusion/Fusion.Runtime/Simulation/Simulation.cs:955)

Fusion.NetworkRunner:UpdateInternal (double) (at Fusion/Fusion.Runtime/Runner/NetworkRunner.cs:1257)

Fusion.NetworkRunner:InvokeUpdate (single) (at Fusion/Fusion.Runtime/Runner/NetworkRunner.Instances.cs:53)

Fusion.NetworkRunnerUpdater:InvokeUpdate () (at Fusion/Fusion.Runtime/Runner/NetworkRunnerUpdater.cs:74)

===============================================================

Back to top