Fish-Net: Networking Evolved
  • Introduction
    • Getting Started
    • Showcase
      • Upcoming Releases
    • Legal Restrictions
    • Pro, Projects, and Support
    • Business Support
    • Branding
  • Manual
    • General
      • Unity Compatibility
      • Changelog
        • Development Road Map
        • Major Version Update Solutions
      • Development
      • Performance
        • Benchmark Setup
        • Fish-Networking Vs Mirror
      • Terminology
        • Miscellaneous
        • Communicating
        • Server, Client, Host
      • Transports
      • Add-ons
        • Edgegap and other Hosting
        • Fish-Network-Discovery
      • Feature Comparison
      • Upgrading To Fish-Networking
    • Guides
      • Frequently Asked Questions (FAQ)
      • Creating Bug Reports
        • Report Example
      • Technical Limitations
      • Third-Party
      • Getting Started
        • Commonly Used Guides
        • Ownership - Moving Soon
        • Step-by-Step
          • Getting Connected
          • Preparing Your Player
      • Components
        • Managers
          • NetworkManager
          • TimeManager
          • PredictionManager
          • ServerManager
          • ClientManager
          • SceneManager
          • TransportManager
            • IntermediateLayer
          • StatisticsManager
          • ObserverManager
            • HashGrid
          • RollbackManager (Pro Feature)
        • Transports
          • FishyWebRTC
          • Bayou
          • FishyEOS (Epic Online Services)
          • FishyFacepunch (Steam)
          • FishyRealtime
          • FishySteamworks (Steam)
          • FishyUnityTransport
          • Multipass
          • Tugboat
          • Yak (Pro Feature)
        • Prediction
          • Network Collider
            • NetworkCollision
            • NetworkCollision2D
            • NetworkTrigger
            • NetworkTrigger2D
          • OfflineRigidbody
          • PredictedOwner
          • PredictedSpawn
        • Utilities
          • Tick Smoothers
            • NetworkTickSmoother
            • OfflineTickSmoother
          • MonoTickSmoother [Obsolete]
          • DetachableNetworkTickSmoother [Obsolete]
          • BandwidthDisplay
          • DefaultScene
          • PingDisplay
        • Authenticator
        • ColliderRollback
        • NetworkAnimator
        • NetworkBehaviour
        • NetworkTransform
        • NetworkObject
        • NetworkObserver
      • InstanceFinder
      • Ownership
        • Using Ownership To Read Values
      • Spawning and Despawning
        • Predicted Spawning
        • Nested NetworkObjects
        • Object Pooling
      • Components
      • NetworkBehaviour
      • NetworkObjects
      • Attributes, Quality of Life
      • Remote Procedure Calls
        • Broadcast
      • SyncTypes
        • Customizing Behavior
        • SyncVar
        • SyncList
        • SyncHashSet
        • SyncDictionary
        • SyncTimer
        • SyncStopwatch
        • Custom SyncType
      • Observers
        • Modifying Conditions
        • Custom Conditions
      • Automatic Serializers
      • Custom Serializers
        • Interface Serializers
        • Inheritance Serializers
      • Addressables
      • Scene Management
        • Scene Events
        • Scene Data
          • SceneLookupData
          • SceneLoadData
          • SceneUnloadData
        • Loading Scenes
        • Unloading Scenes
        • Scene Stacking
        • Scene Caching
        • Scene Visibility
        • Persisting NetworkObjects
        • Custom Scene Processors
          • Addressables
      • Transports
        • Multipass
      • Prediction
        • What Is Client-Side Prediction
        • Configuring PredictionManager
        • Configuring TimeManager
        • Configuring NetworkObject
        • Offline Rigidbodies
        • Interpolations
        • Creating Code
          • Controlling An Object
          • Non-Controlled Object
          • Understanding ReplicateState
            • Using States In Code
            • Predicting States In Code
          • Advanced Controls
        • Custom Comparers
        • PredictionRigidbody
        • Using NetworkColliders
      • Lag Compensation
        • States
        • Raycast
        • Projectiles
    • Server Hosting
      • Edgegap - Official Partner
        • Getting Started with Edgegap
      • Hathora
        • Getting Started with Hathora
      • Amazon Web Services (AWS)
        • Getting Started with AWS
    • API
Powered by GitBook
On this page
  • Callbacks
  • Usage
  1. Manual
  2. Guides
  3. Prediction

Using NetworkColliders

Using each NetworkCollider component is the same, and can be used very similar to Unity callbacks.

When using client-side prediction the NetworkCollider components are designed to accurately dispatch Enter, Stay, and Exit callbacks for collisions and triggers.

All events have the same name across each component: NetworkCollider, NetworkCollider2D, NetworkTrigger, and NetworkTrigger2D. What is returned in each event naturally will vary, depending on which component you use. For example, the NetworkCollision component will return Collider for what was intersected, while NetworkCollision2D will return Collider2D.

Our OnStay callback does not return Collision as you would expect with the Unity version. Due to Unity limitations we are unable to provide a Collision callback without hotpath allocations.

If you require Collision to be returned please use the Unity OnStay callback instead; this will function properly with prediction.

Callbacks

OnEnter is invoked when this collider has entered the bounds of another collider.

OnStay is invoked when this collider remains in contact with another.

OnExit is invoked when this collider has exited the bounds of another collider.

Keep in mind each callback description varies if you are using Collsion, Trigger, or their 2D counterparts. None-the-less, they behave just like Unity callbacks.

Usage

This example script is placed on the player object. Our script demonstrates subscribing to all three events and using the OnEnter event to play a sound, as well applying forces to 'this' player.

//We are assuming this script inherits NetworkBehaviour.
[SerializeField]
private AudioSource _hitSound;

private NetworkCollision _networkCollision;

private void Awake()
{
    //Get the NetworkCollision component placed on this object.
    //You can place the component anywhere you would normally
    //use Unity collider callbacks!
    _networkCollision = GetComponent<NetworkCollision>();
    // Subscribe to the desired collision event
    _networkCollision.OnEnter += NetworkCollisionEnter;
    _networkCollision.OnStay += NetworkCollisionStay;
    _networkCollision.OnExit += NetworkCollisionExit;
}

private void OnDestroy()
{
    //Since the NetworkCollider is placed on the same object as
    //this script we do not need to unsubscribe; the callbacks
    //will be destroyed with the object.
    //
    //But if your NetworkCollider resides on another object you
    //likely will want to unsubscribe to your events as well as shown.
    if (_networkCollision != null)
    {
        _networkCollision.OnEnter -= NetworkCollisionEnter;
        _networkCollision.OnStay -= NetworkCollisionStay;
        _networkCollision.OnExit -= NetworkCollisionExit;
    }
}

private void NetworkCollisionEnter(Collider other)
{
    //Only play the sound effect when not currently reconciling.
    //If you were to play the sound also during reconcilations
    //the audio would execute each reconcile, each tick, until the
    //player was no longer reconciling into the collider.
    if (!base.PredictionManager.IsReconciling)
        _hitSound.Play();
        
    //Always apply velocity to this player on enter, even if reconciling.
    PlayerMover pm = GetComponent<PlayerMover>();
    //For this example we are pushing away from the other object.
    Vector3 dir = (transform.position - other.gameObject.transform).normalized;
    pm.PredictionRigidbody.AddForce(dir * 50f, ForceMode.Impulse);
}

private void NetworkCollisionStay(Collider other)
{
    // Handle collision stay logic
}

private void NetworkCollisionExit(Collider other)
{
    // Handle collision exit logic (e.g., deactivate visual effects)
}
PreviousPredictionRigidbodyNextLag Compensation

Last updated 12 months ago

You may be wondering what is PredictionRigidbody and why did we apply forces to that instead of the Rigidbody directly. PredictionRigidbody(and 2D) is a mandatory but still simple way to apply forces to predicted rigidbodies. You can learn more about it's usage .

here