Skip to main content

Lag Compensated Hitbox & Raycasts

Expected Output

  • Quick guide about hitbox and raycasts, what they do, and how they impact in networked games.

Key Solution

Hitbox

Brief Fusion Hitbox Explanation

  • Hitboxes are a set of 2D or 3D shapes that a game uses to register real-time collisions, as for fusion hitbox itself behave similar like that, but has additional element n built in it. fusion hitbox aim to solve one of fundamental networked games (Lag Compensation), add precision on collision detection in network games, thus should improving gaming experience overall.
  • There are only two types of shapes for fusion hitbox, box or sphere.
  • Fusion Hitbox can be interacted by casting fusion raycasts, detecting if ray hit it.
  • Fusion Hitbox should be active before it can interact with raycasts.
  • Fusion Hitbox work in tandem with regular Unity Collider, regular collider allow object to interact with Physics while fusion hitbox allow lag compensation works with that object (assuming that object is networked).

Fusion Hitbox Setup

  • UploadAs Infor Progresshow to set up hitbox, below requirement cited from Photon Lag Compensation page.

    To set up a lag compensated hitbox on a networked GameObject, the following two steps are required:

    1. HitBoxRoot component is needed on the top most node of the GameObject. The HitBoxRoot serves as a way to group all HitBox components found on its children.
    2. HitBox nodes should be kept in a separate layer from regular Unity Collider components for all dynamic objects; this is the fastest and only reliable way to separate dynamic Colliders from Hitboxes when raycasting. This separation allows lag compensated raycasts to hit all static geometry while avoiding the need to add HitBox  components which would be prohibitively expensive. At the same time all dynamic hits should rely exclusively on Hitboxes, but removing dynamic Colliders entirely does not work because they are needed to allow objects to interact nicely with PhysX. The middle ground is to keep the dynamic colliders on a layer that can be ignored by lag compensated ray-casts.

    N.B.: There is a limit of 31 HitBox nodes per HitBoxRoot. If there is a need for more child nodes in a single object or prefab, the hierarchy must be broken down and distributed across several roots.

    The specific structure of the hit-box hierarchy is entirely up to the needs of the specific game.

Raycasts

Brief Fusion Raycasts Explanation

  • Similar with unity Raycasts, fusion lag compensated raycasts create a ray from a point to another and check specified object collide with it. Fusion raycasts also support OverlapSphere, OverlapBox, RaycastAll for more complex Raycasts detection.
  • Fusion Raycasts only interact with fusion hitbox, since using the unity version should be preferred if interacting with a non-networked object.
  • The raycasts will be done against the data matching the timeframe viewed by the specific player client who was in control of it, and be done automatically.

Using Fusion Lag Compensated Raycasts

  • Performing Fusion Raycasts is as simple as normal raycasts in Unity, however since its designed with Networking in mind, its works best in networked environment. below some lag compensated raycasts function.

    Runner.LagCompensation.Raycast(transform.position, transform.forward, 25, Object.InputAuthority, out var hit);
    
    int numOfHits = Runner.LagCompensation.OverlapBox(transform.position, transform.position + offset, Quaternion.Identity, Object.InputAuthority, hits);
    
    int numOfHits = Runner.LagCompensation.OverlapSphere(transform.position, radius, Object.InputAuthority, hits);
    
  • Fusion OverlapBox and OverlapSphere store their hits results in an already defined list of LagCompensatedHit, making both functions work like their unity counterpart but the non-alloc version with list instead of array.

Demo

  • Please refer to [Sync Animation Page](Sync Animation Handling 7dd9051499c349f4bd068ca11119b092.md) for one of implementation of raycasts and hitbox.