Colliders
Summary
Shows how to modify colliders at runtime and sync the changes to clients using properties.
Collider scripts
Each collider has an associated server entity collider script. To get the list of colliders on an entity, use
Scripts.GetAll<ksCollider>()
. Once you have a reference to a collider script, you can modify its properties.
Enabling/disabling colliders at runtime
There are multiple different ways to enable/disable a collider, as described below.
ksCollider.IsEnabled
- Enable or disable a collider by setting this. Disabled collider scripts remain attached to their entities, but no longer collider or generate events. It's best to use this is you are temporarily disabling the collider and will enable it again later.Scripts.Attach
andScripts.Detach
- Attach or detach collider scripts to add new colliders or completely remove colliders. It's best to detach a collider when you know you will never use it again.ksCollider.CollisionFilter
- You can change the collision filter on the collider to change which collision groups it collides with. Use this when you want to disable collisions with some objects, but still allow collisions with some other objects. See here for more information about using collision filters.ksCollider.IsTrigger
- Setting this to true will make the collider collide with nothing. Use this when you want to disable all collisions, but still want to receive overlap events for the collider.ksCollider.IsSimulationCollider
- Setting this to false will stop the collider from colliding or generating events, but it can still be found using scene queries ifksCollider.IsQueryCollider
is true. Use this when you want to disable all collisions and events, but you want the collider to appear in scene queries (such as raycasts, sweeps, or overlaps).Syncing collider changes
Modifications to collider scripts do not automatically sync to clients. If you are using a predictor on the client that predicts collisions such as the ksConvergingInputPredictor (this is the default for player-controlled entities that use input prediction), you will need to sync these changes yourself to make the collision prediction work properly. The following example shows how to use a property to sync collider enabled state.
Server Collider Controller Entity Script
using System.Collections.Generic;
using KS.Reactor.Server;
public class sColliderController : ksServerEntityScript
{
public override void Initialize()
{
SyncColliderState();
}
// Syncs the enabled state of all colliders as a bool array property. Call this after enabling or disabling colliders.
public void SyncColliderState()
{
List<ksCollider> colliders = Scripts.GetAll<ksCollider>();
bool[] states = new bool[colliders.Count];
for (int i = 0; i < colliders.Count; i++)
{
states[i] = colliders[i].IsEnabled;
}
Properties[0] = states;
}
}
Client Collider Controller Entity Script
using UnityEngine;
using KS.Reactor.Client.Unity;
using KS.Reactor;
public class cColliderController : ksEntityScript
{
public override void Initialize()
{
// When the property changes, change the enabled state of colliders.
Entity.OnPropertyChange[0] += OnEnabledChange;
OnEnabledChange(false, Properties[0]);
}
public override void Detached()
{
Entity.OnPropertyChange[0] -= OnEnabledChange;
}
private void OnEnabledChange(ksMultiType oldValue, ksMultiType newValue)
{
bool[] states = newValue;
if (states == null)
{
return;
}
Collider[] colliders = GetComponents<Collider>();
for (int i = 0; i < colliders.Length; i++)
{
colliders[i].enabled = i < states.Length && states[i];
}
}
}