Skip to content

Instantly share code, notes, and snippets.

@luna-duclos
Last active February 4, 2019 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luna-duclos/dc06674a877be04cece8788d3d3bae82 to your computer and use it in GitHub Desktop.
Save luna-duclos/dc06674a877be04cece8788d3d3bae82 to your computer and use it in GitHub Desktop.
NavAgent synchronization with a game client using SpatialOS's TransformSynchronization module
using Improbable.Gdk.TransformSynchronization;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Transforms;
using UnityEngine;
namespace Faeborn.NavSystem
{
public class NavAgentToImprobableTransformSyncSystem : JobComponentSystem
{
[BurstCompile]
private struct NavAgentToImprobableTransformSyncSystemJob : IJobProcessComponentData<TransformToSend, NavAgent, SyncToImprobableTransformTag>
{
public void Execute (ref TransformToSend TransformToSend, [ReadOnly] ref NavAgent NavAgent, [ReadOnly] ref SyncToImprobableTransformTag SyncToImprobableTransformTag)
{
TransformToSend.Position = NavAgent.position - SyncToImprobableTransformTag.origin;
TransformToSend.Orientation = NavAgent.rotation;
TransformToSend.Velocity = NavAgent.rotation * Vector3.forward * NavAgent.moveSpeed;
}
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
return new NavAgentToImprobableTransformSyncSystemJob().Schedule(this, inputDeps);
}
}
}
using Improbable;
using Improbable.Gdk.Core;
using Improbable.Gdk.TransformSynchronization;
using Improbable.Transform;
using Unity.Collections;
using Unity.Entities;
using Quaternion = UnityEngine.Quaternion;
namespace Faeborn.NavSystem
{
[DisableAutoCreation]
public class InitializationSystem : ComponentSystem
{
private struct Data
{
public readonly int Length;
[ReadOnly] public EntityArray Entities;
[ReadOnly] public ComponentDataArray<Metadata.Component> Metadata;
[ReadOnly] public ComponentDataArray<Position.Component> Position;
[ReadOnly] public ComponentDataArray<TransformInternal.Component> Transform;
[ReadOnly] public ComponentDataArray<NewlyAddedSpatialOSEntity> DenotesNewSpatialOSEntity;
}
private readonly World world;
private readonly NavSettings navSettings;
private readonly TransformSynchronizationSendStrategy sendStrategy;
[Inject] private EntityManager entityManager;
[Inject] private WorkerSystem workerSystem;
[Inject] private Data data;
public NavInitializationSystem(World world, NavSettings navSettings, TransformSynchronizationSendStrategy sendStrategy)
{
this.world = world;
this.navSettings = navSettings;
this.sendStrategy = sendStrategy;
}
protected override void OnUpdate()
{
for (var i = 0; i < data.Length; i++)
{
switch (data.Metadata[i].EntityType)
{
case "Player":
var entity = data.Entities[i];
var transform = data.Transform[i];
// Navigation components
PostUpdateCommands.AddComponent(entity, new NavAgent(
transform.Location.ToUnityVector3(),
Quaternion.identity,
navSettings.StoppingDistance,
navSettings.MoveSpeed,
navSettings.Acceleration,
navSettings.RotationSpeed,
navSettings.DefaultAreaMask));
PostUpdateCommands.AddComponent(entity, new SyncToImprobableTransformTag{origin = workerSystem.Origin});
// Improbable TransformSynchronization components
PostUpdateCommands.AddComponent(entity, new TransformToSend
{
Position = transform.Location.ToUnityVector3() - workerSystem.Origin,
Velocity = transform.Velocity.ToUnityVector3(),
Orientation = transform.Rotation.ToUnityQuaternion()
});
PostUpdateCommands.AddComponent(entity, new TicksSinceLastTransformUpdate { NumberOfTicks = 0 });
PostUpdateCommands.AddComponent(entity, new LastTransformSentData
{
// could set this to the max time if we want to immediately send something
TimeSinceLastUpdate = 0.0f,
Transform = data.Transform[i],
});
PostUpdateCommands.AddComponent(entity, new LastPositionSentData
{
// could set this to the max time if we want to immediately send something
TimeSinceLastUpdate = 0.0f,
Position = data.Position[i],
});
sendStrategy.Apply(entity, world, PostUpdateCommands);
break;
}
}
}
}
}
using Unity.Entities;
using Unity.Mathematics;
namespace Faeborn.NavSystem
{
public struct SyncToImprobableTransformTag : IComponentData
{
public float3 origin;
}
}
@luna-duclos
Copy link
Author

NavAgent module was taken from github and nearly 100% used as is with some very minor modifications: https://github.com/zulfajuniadi/unity-ecs-navmesh/

@luna-duclos
Copy link
Author

Above code may be considered MIT licensed by anyone that finds this link and used within the restrictions of that license.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment