Progress character movement

This commit is contained in:
Lauchstange committed 2025-02-03 20:30:57 +01:00
1 parent 08e27ca2f1
commit 01f38a19a1
10 files changed
+160 -29

No files matched your search

@@ -4,6 +4,7 @@ using Unity.Entities;
using Unity.Mathematics;
using UnityEngine;
using Unity.CharacterController;
using Unity.NetCode;
[Serializable]
public struct FirstPersonCharacterComponent : IComponentData
@@ -22,7 +23,7 @@ public struct FirstPersonCharacterComponent : IComponentData
public float MaxViewAngle;
public Entity ViewEntity;
public float ViewPitchDegrees;
[GhostField] public float ViewPitchDegrees;
public quaternion ViewLocalRotation;
}
@@ -7,6 +7,7 @@ using Unity.Physics;
using Unity.Transforms;
using Unity.CharacterController;
using Unity.Burst.Intrinsics;
using Unity.NetCode;
[UpdateInGroup(typeof(KinematicCharacterPhysicsUpdateGroup))]
[BurstCompile]
@@ -72,10 +73,9 @@ public partial struct FirstPersonCharacterPhysicsUpdateSystem : ISystem
}
}
[UpdateInGroup(typeof(SimulationSystemGroup))]
[UpdateAfter(typeof(FixedStepSimulationSystemGroup))]
[UpdateInGroup(typeof(PredictedSimulationSystemGroup))]
[UpdateAfter(typeof(PredictedFixedStepSimulationSystemGroup))]
[UpdateAfter(typeof(FirstPersonPlayerVariableStepControlSystem))]
[UpdateBefore(typeof(TransformSystemGroup))]
[BurstCompile]
public partial struct FirstPersonCharacterVariableUpdateSystem : ISystem
{
@@ -1,18 +1,26 @@
using System;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.NetCode;
[Serializable]
public struct FirstPersonPlayer : IComponentData
{
public Entity ControlledCharacter;
[GhostField] public Entity ControlledCharacter;
}
[Serializable]
public struct FirstPersonPlayerInputs : IComponentData
public struct FirstPersonPlayerInputs : IInputComponentData
{
public float2 MoveInput;
public float2 LookInput;
public FixedInputEvent JumpPressed;
}
[Serializable]
[GhostComponent(SendTypeOptimization = GhostSendType.OnlyPredictedClients)]
public struct FirstPersonPlayerNetworkInput : IComponentData
{
[GhostField()]
public float2 LastProcessedLookInput;
}
@@ -16,6 +16,7 @@ public class FirstPersonPlayerAuthoring : MonoBehaviour
ControlledCharacter = GetEntity(authoring.ControlledCharacter, TransformUsageFlags.Dynamic),
});
AddComponent<FirstPersonPlayerInputs>(entity);
AddComponent<FirstPersonPlayerNetworkInput>(entity);
}
}
}
@@ -1,26 +1,26 @@
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
using Unity.CharacterController;
using Unity.NetCode;
[UpdateInGroup(typeof(InitializationSystemGroup))]
[UpdateInGroup(typeof(GhostInputSystemGroup))]
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
public partial class FirstPersonPlayerInputsSystem : SystemBase
{
protected override void OnCreate()
{
RequireForUpdate<FixedTickSystem.Singleton>();
RequireForUpdate<NetworkTime>();
RequireForUpdate(SystemAPI.QueryBuilder().WithAll<FirstPersonPlayer, FirstPersonPlayerInputs>().Build());
}
protected override void OnUpdate()
{
uint tick = SystemAPI.GetSingleton<FixedTickSystem.Singleton>().Tick;
foreach (var (playerInputs, player) in SystemAPI.Query<RefRW<FirstPersonPlayerInputs>, FirstPersonPlayer>())
var time = SystemAPI.GetSingleton<FixedTickSystem.Singleton>();
foreach (var (playerInputs, player) in SystemAPI.Query<RefRW<FirstPersonPlayerInputs>, FirstPersonPlayer>().WithAll<GhostOwnerIsLocal>())
{
playerInputs.ValueRW.MoveInput = new float2
{
@@ -28,11 +28,13 @@ public partial class FirstPersonPlayerInputsSystem : SystemBase
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"));
InputDeltaUtilities.AddInputDelta(ref playerInputs.ValueRW.LookInput.x, Input.GetAxis("Mouse X"));
InputDeltaUtilities.AddInputDelta(ref playerInputs.ValueRW.LookInput.y, Input.GetAxis("Mouse Y"));
playerInputs.ValueRW.JumpPressed = default;
if (Input.GetKeyDown(KeyCode.Space))
{
playerInputs.ValueRW.JumpPressed.Set(tick);
playerInputs.ValueRW.JumpPressed.Set(time.Tick);
}
}
}
@@ -41,28 +43,33 @@ public partial class FirstPersonPlayerInputsSystem : SystemBase
/// <summary>
/// Apply inputs that need to be read at a variable rate
/// </summary>
[UpdateInGroup(typeof(SimulationSystemGroup))]
[UpdateAfter(typeof(FixedStepSimulationSystemGroup))]
[UpdateInGroup(typeof(PredictedSimulationSystemGroup))]
[UpdateAfter(typeof(PredictedFixedStepSimulationSystemGroup))]
[BurstCompile]
public partial struct FirstPersonPlayerVariableStepControlSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<NetworkTime>();
state.RequireForUpdate(SystemAPI.QueryBuilder().WithAll<FirstPersonPlayer, FirstPersonPlayerInputs>().Build());
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
foreach (var (playerInputs, player) in SystemAPI.Query<FirstPersonPlayerInputs, FirstPersonPlayer>().WithAll<Simulate>())
foreach (var (playerInputs, playerNetworkInput, player) in SystemAPI.Query<FirstPersonPlayerInputs, RefRW<FirstPersonPlayerNetworkInput>, FirstPersonPlayer>().WithAll<Simulate>())
{
// Compute input deltas, compared to last known values
float2 lookInputDelta = InputDeltaUtilities.GetInputDelta(
playerInputs.LookInput,
playerNetworkInput.ValueRO.LastProcessedLookInput);
playerNetworkInput.ValueRW.LastProcessedLookInput = playerInputs.LookInput;
if (SystemAPI.HasComponent<FirstPersonCharacterControl>(player.ControlledCharacter))
{
FirstPersonCharacterControl characterControl = SystemAPI.GetComponent<FirstPersonCharacterControl>(player.ControlledCharacter);
characterControl.LookDegreesDelta = playerInputs.LookInput;
characterControl.LookDegreesDelta = lookInputDelta;
SystemAPI.SetComponent(player.ControlledCharacter, characterControl);
}
}
@@ -73,22 +80,23 @@ public partial struct FirstPersonPlayerVariableStepControlSystem : ISystem
/// 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.
/// </summary>
[UpdateInGroup(typeof(FixedStepSimulationSystemGroup), OrderFirst = true)]
[UpdateInGroup(typeof(PredictedFixedStepSimulationSystemGroup), OrderFirst = true)]
[BurstCompile]
public partial struct FirstPersonPlayerFixedStepControlSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<FixedTickSystem.Singleton>();
state.RequireForUpdate<NetworkTime>();
state.RequireForUpdate(SystemAPI.QueryBuilder().WithAll<FirstPersonPlayer, FirstPersonPlayerInputs>().Build());
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
uint tick = SystemAPI.GetSingleton<FixedTickSystem.Singleton>().Tick;
var time = SystemAPI.GetSingleton<FixedTickSystem.Singleton>();
foreach (var (playerInputs, player) in SystemAPI.Query<FirstPersonPlayerInputs, FirstPersonPlayer>().WithAll<Simulate>())
{
if (SystemAPI.HasComponent<FirstPersonCharacterControl>(player.ControlledCharacter))
@@ -104,7 +112,7 @@ public partial struct FirstPersonPlayerFixedStepControlSystem : ISystem
characterControl.MoveVector = MathUtilities.ClampToMaxLength(characterControl.MoveVector, 1f);
// Jump
characterControl.Jump = playerInputs.JumpPressed.IsSet(tick);
characterControl.Jump = playerInputs.JumpPressed.IsSet(time.Tick);
SystemAPI.SetComponent(player.ControlledCharacter, characterControl);
}