using Unity.Burst; using Unity.Collections; using Unity.Entities; using Unity.Jobs; using Unity.Mathematics; using Unity.Transforms; using UnityEngine; using Unity.CharacterController; [UpdateInGroup(typeof(InitializationSystemGroup))] public partial class FirstPersonPlayerInputsSystem : SystemBase { protected override void OnCreate() { RequireForUpdate(); RequireForUpdate(SystemAPI.QueryBuilder().WithAll().Build()); } protected override void OnUpdate() { uint tick = SystemAPI.GetSingleton().Tick; foreach (var (playerInputs, player) in SystemAPI.Query, FirstPersonPlayer>()) { playerInputs.ValueRW.MoveInput = new float2 { x = (Input.GetKey(KeyCode.D) ? 1f : 0f) + (Input.GetKey(KeyCode.A) ? -1f : 0f), y = (Input.GetKey(KeyCode.W) ? 1f : 0f) + (Input.GetKey(KeyCode.S) ? -1f : 0f), }; playerInputs.ValueRW.LookInput = new float2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); if (Input.GetKeyDown(KeyCode.Space)) { playerInputs.ValueRW.JumpPressed.Set(tick); } } } } /// /// Apply inputs that need to be read at a variable rate /// [UpdateInGroup(typeof(SimulationSystemGroup))] [UpdateAfter(typeof(FixedStepSimulationSystemGroup))] [BurstCompile] public partial struct FirstPersonPlayerVariableStepControlSystem : ISystem { [BurstCompile] public void OnCreate(ref SystemState state) { state.RequireForUpdate(SystemAPI.QueryBuilder().WithAll().Build()); } [BurstCompile] public void OnUpdate(ref SystemState state) { foreach (var (playerInputs, player) in SystemAPI.Query().WithAll()) { if (SystemAPI.HasComponent(player.ControlledCharacter)) { FirstPersonCharacterControl characterControl = SystemAPI.GetComponent(player.ControlledCharacter); characterControl.LookDegreesDelta = playerInputs.LookInput; SystemAPI.SetComponent(player.ControlledCharacter, characterControl); } } } } /// /// Apply inputs that need to be read at a fixed rate. /// It is necessary to handle this as part of the fixed step group, in case your framerate is lower than the fixed step rate. /// [UpdateInGroup(typeof(FixedStepSimulationSystemGroup), OrderFirst = true)] [BurstCompile] public partial struct FirstPersonPlayerFixedStepControlSystem : ISystem { [BurstCompile] public void OnCreate(ref SystemState state) { state.RequireForUpdate(); state.RequireForUpdate(SystemAPI.QueryBuilder().WithAll().Build()); } [BurstCompile] public void OnUpdate(ref SystemState state) { uint tick = SystemAPI.GetSingleton().Tick; foreach (var (playerInputs, player) in SystemAPI.Query().WithAll()) { if (SystemAPI.HasComponent(player.ControlledCharacter)) { FirstPersonCharacterControl characterControl = SystemAPI.GetComponent(player.ControlledCharacter); quaternion characterRotation = SystemAPI.GetComponent(player.ControlledCharacter).Rotation; // Move float3 characterForward = MathUtilities.GetForwardFromRotation(characterRotation); float3 characterRight = MathUtilities.GetRightFromRotation(characterRotation); characterControl.MoveVector = (playerInputs.MoveInput.y * characterForward) + (playerInputs.MoveInput.x * characterRight); characterControl.MoveVector = MathUtilities.ClampToMaxLength(characterControl.MoveVector, 1f); // Jump characterControl.Jump = playerInputs.JumpPressed.IsSet(tick); SystemAPI.SetComponent(player.ControlledCharacter, characterControl); } } } }