Hello, I am currently a noob for DOTS and learning the job system with ECS and the Input System. I am currently generating an entity with the default cube mesh and I want it to interact with the new Input System package (currently 1.0.1). If anyone knows how to implement the new Input System with Jobs and ECS, I would be really grateful to you.
Thanks in advance.
This is probably the most basic implementation to marry InputSystem
with ecs now:
// PlayerAuthoring.cs
using UnityEngine;
using UnityEngine.InputSystem;
using Unity.Mathematics;
using Unity.Entities;
using Unity.Transforms;
public class PlayerAuthoring : MonoBehaviour, IConvertGameObjectToEntity
{
void IConvertGameObjectToEntity.Convert ( Entity entity , EntityManager dstManager , GameObjectConversionSystem conversionSystem )
{
dstManager.AddComponent<PLAYER>( entity );
}
}
public struct PLAYER : IComponentData {}
public class PlayerMovementSystem : SystemBase
{
protected override void OnUpdate ()
{
float dt = Time.DeltaTime;
float3 inputs = default(float3);
const float speed = 5f;
// replace this code with your own input mapping method:
var keyboard = Keyboard.current;
if( keyboard!=null )
{
inputs = new float3{
x = keyboard.dKey.ReadValue() - keyboard.aKey.ReadValue() ,
y = keyboard.spaceKey.ReadValue() - keyboard.leftCtrlKey.ReadValue() ,
z = keyboard.wKey.ReadValue() - keyboard.sKey.ReadValue()
};
}
Entities
.WithName("basic_locomotion_job")
.WithAll<PLAYER>()
.ForEach( ( ref LocalToWorld transform , in Entity entity ) =>
{
#if UNITY_EDITOR
Debug.DrawLine( transform.Position , transform.Position+inputs , Color.yellow );
#endif
transform.Value = math.mul(
transform.Value ,
float4x4.Translate( math.normalizesafe(inputs) * speed * dt )
);
} )
.WithBurst().ScheduleParallel();
}
}