1- using Arch . Core ;
1+ using Arch . Core ;
22using Arch . System ;
33using Arch . SystemGroups ;
44using Arch . SystemGroups . DefaultSystemGroups ;
5+ using DCL . AvatarRendering . DemoScripts . Components ;
56using DCL . Character . CharacterMotion . Components ;
67using DCL . Character . CharacterMotion . Velocity ;
78using DCL . CharacterCamera ;
9+ using DCL . CharacterMotion ;
810using DCL . CharacterMotion . Components ;
911using DCL . CharacterMotion . Settings ;
10- using DCL . DebugUtilities ;
11- using DCL . DebugUtilities . UIBindings ;
1212using DCL . Time . Systems ;
1313using ECS . Abstract ;
1414using ECS . LifeCycle . Components ;
1515using UnityEngine ;
16- using DCL . AvatarRendering . DemoScripts . Components ;
1716using DCL . CharacterMotion . Utils ;
1817
19- namespace DCL . CharacterMotion . Systems
18+ namespace DCL . Character . CharacterMotion . Systems
2019{
2120 /// <summary>
2221 /// Entry point to calculate everything that affects character's velocity
@@ -27,107 +26,17 @@ public partial class CalculateCharacterVelocitySystem : BaseUnityLoopSystem
2726 {
2827 private SingleInstanceEntity camera ;
2928 private SingleInstanceEntity fixedTick ;
30- private SingleInstanceEntity entitySettings ;
31-
32- private readonly ElementBinding < float > cameraRunFov = new ( 0 ) ;
33- private readonly ElementBinding < float > walkSpeed = new ( 0 ) ;
34- private readonly ElementBinding < float > jogSpeed = new ( 0 ) ;
35- private readonly ElementBinding < float > runSpeed = new ( 0 ) ;
36- private readonly ElementBinding < float > jogJumpHeight = new ( 0 ) ;
37- private readonly ElementBinding < float > runJumpHeight = new ( 0 ) ;
38- private readonly ElementBinding < float > jumpHold = new ( 0 ) ;
39- private readonly ElementBinding < float > jumpHoldGravity = new ( 0 ) ;
40- private readonly ElementBinding < float > gravity = new ( 0 ) ;
41- private readonly ElementBinding < float > airAcc = new ( 0 ) ;
42- private readonly ElementBinding < float > maxAirAcc = new ( 0 ) ;
43- private readonly ElementBinding < float > airDrag = new ( 0 ) ;
44- private readonly ElementBinding < float > stopTime = new ( 0 ) ;
45-
46- private readonly ElementBinding < int > airJumpCount = new ( 0 ) ;
47- private readonly ElementBinding < float > airJumpHeight = new ( 0 ) ;
48- private readonly ElementBinding < float > airJumpDelay = new ( 0 ) ;
49- private readonly ElementBinding < float > airJumpGravityDuringDelay = new ( 0 ) ;
50- private readonly ElementBinding < float > cooldownBetweenJumps = new ( 0 ) ;
51- private readonly ElementBinding < float > airJumpImpulse = new ( 0 ) ;
52-
53- public CalculateCharacterVelocitySystem ( World world , IDebugContainerBuilder debugBuilder ) : base ( world )
54- {
55- debugBuilder . TryAddWidget ( "Locomotion: Base" ) ?
56- . AddFloatField ( "Camera Run FOV" , cameraRunFov )
57- . AddFloatField ( "Walk Speed" , walkSpeed )
58- . AddFloatField ( "Jog Speed" , jogSpeed )
59- . AddFloatField ( "Run Speed" , runSpeed )
60- . AddFloatField ( "Jog Jump Height" , jogJumpHeight )
61- . AddFloatField ( "Run Jump Height" , runJumpHeight )
62- . AddFloatField ( "Jump Hold Time" , jumpHold )
63- . AddFloatField ( "Jump Hold Gravity Scale" , jumpHoldGravity )
64- . AddFloatField ( "Gravity" , gravity )
65- . AddFloatField ( "Air Acceleration" , airAcc )
66- . AddFloatField ( "Max Air Acceleration" , maxAirAcc )
67- . AddFloatField ( "Air Drag" , airDrag )
68- . AddFloatField ( "Grounded Stop Time" , stopTime ) ;
69-
70- debugBuilder . TryAddWidget ( "Locomotion: Air Jumping" ) ?
71- . AddControl ( new DebugConstLabelDef ( "Air Jump Count" ) , new DebugIntFieldDef ( airJumpCount ) )
72- . AddFloatField ( "Height" , airJumpHeight )
73- . AddFloatField ( "Delay" , airJumpDelay )
74- . AddFloatField ( "Gravity during Delay" , airJumpGravityDuringDelay )
75- . AddFloatField ( "Cooldown" , cooldownBetweenJumps )
76- . AddFloatField ( "Direction Change Impulse" , airJumpImpulse ) ;
77- }
29+
30+ public CalculateCharacterVelocitySystem ( World world ) : base ( world ) { }
7831
7932 public override void Initialize ( )
8033 {
8134 camera = World . CacheCamera ( ) ;
8235 fixedTick = World . CachePhysicsTick ( ) ;
83- entitySettings = World . CacheCharacterSettings ( ) ;
84-
85- ICharacterControllerSettings settings = entitySettings . GetCharacterSettings ( World ) ;
86- cameraRunFov . Value = settings . CameraFOVWhileRunning ;
87- walkSpeed . Value = settings . WalkSpeed ;
88- jogSpeed . Value = settings . JogSpeed ;
89- runSpeed . Value = settings . RunSpeed ;
90- jogJumpHeight . Value = settings . JogJumpHeight ;
91- runJumpHeight . Value = settings . RunJumpHeight ;
92- jumpHold . Value = settings . LongJumpTime ;
93- jumpHoldGravity . Value = settings . LongJumpGravityScale ;
94- gravity . Value = settings . Gravity ;
95- airAcc . Value = settings . AirAcceleration ;
96- maxAirAcc . Value = settings . MaxAirAcceleration ;
97- airDrag . Value = settings . AirDrag ;
98- stopTime . Value = settings . StopTimeSec ;
99- airJumpCount . Value = settings . AirJumpCount ;
100- airJumpHeight . Value = settings . AirJumpHeight ;
101- airJumpDelay . Value = settings . AirJumpDelay ;
102- airJumpGravityDuringDelay . Value = settings . AirJumpGravityDuringDelay ;
103- cooldownBetweenJumps . Value = settings . CooldownBetweenJumps ;
104- airJumpImpulse . Value = settings . AirJumpDirectionChangeImpulse ;
10536 }
10637
10738 protected override void Update ( float t )
10839 {
109- ICharacterControllerSettings settings = entitySettings . GetCharacterSettings ( World ) ;
110-
111- settings . CameraFOVWhileRunning = cameraRunFov . Value ;
112- settings . WalkSpeed = walkSpeed . Value ;
113- settings . JogSpeed = jogSpeed . Value ;
114- settings . RunSpeed = runSpeed . Value ;
115- settings . JogJumpHeight = jogJumpHeight . Value ;
116- settings . RunJumpHeight = runJumpHeight . Value ;
117- settings . LongJumpTime = jumpHold . Value ;
118- settings . LongJumpGravityScale = jumpHoldGravity . Value ;
119- settings . Gravity = gravity . Value ;
120- settings . AirAcceleration = airAcc . Value ;
121- settings . MaxAirAcceleration = maxAirAcc . Value ;
122- settings . AirDrag = airDrag . Value ;
123- settings . StopTimeSec = stopTime . Value ;
124- settings . AirJumpCount = airJumpCount . Value ;
125- settings . AirJumpHeight = airJumpHeight . Value ;
126- settings . AirJumpDelay = airJumpDelay . Value ;
127- settings . AirJumpGravityDuringDelay = airJumpGravityDuringDelay . Value ;
128- settings . CooldownBetweenJumps = cooldownBetweenJumps . Value ;
129- settings . AirJumpDirectionChangeImpulse = airJumpImpulse . Value ;
130-
13140 ResolveVelocityQuery ( World , t , fixedTick . GetPhysicsTickComponent ( World ) . Tick , in camera . GetCameraComponent ( World ) ) ;
13241 ResolveRandomAvatarVelocityQuery ( World , t , fixedTick . GetPhysicsTickComponent ( World ) . Tick , in camera . GetCameraComponent ( World ) ) ;
13342 }
@@ -213,19 +122,29 @@ private void ResolveAvatarVelocity([Data] float dt,
213122 // Apply velocity multiplier based on walls
214123 ApplyWallSlide . Execute ( ref rigidTransform , characterController , in settings ) ;
215124
216- // Apply vertical velocity
125+ // External forces must run before gravity so ExternalAcceleration.y is available
126+ ApplyExternalForce . Execute ( settings , ref rigidTransform , dt ) ;
127+
128+ // Vertical velocity (jump + gravity with effective gravity from external forces)
217129 ApplyJump . Execute ( settings , ref rigidTransform , ref jumpState , ref jumpInput , in movementInput , viewerForward , viewerRight , physicsTick , dt ) ;
218130 ApplyGravity . Execute ( settings , ref rigidTransform , jumpState , in jumpInput , physicsTick , dt ) ;
219131 ApplyGliding . Execute ( settings , in rigidTransform , jumpState , in jumpInput , ref glideState , physicsTick , dt ) ;
220132
221- ApplyAirDrag . Execute ( settings , ref rigidTransform , dt ) ;
133+ // External impulses must run after gravity so it nullify gravity velocity.y
134+ ApplyExternalImpulse . Execute ( settings , ref rigidTransform ) ;
135+
136+ // Drag
137+ ApplyHorizontalAirDrag . Execute ( settings , ref rigidTransform , dt ) ;
138+ ApplyExternalVelocityDragAndClamp . Execute ( settings , ref rigidTransform , dt ) ;
222139
140+ // Rotation
223141 if ( cameraComponent . Mode == CameraMode . FirstPerson )
224142 ApplyFirstPersonRotation . Execute ( ref rigidTransform , in cameraComponent ) ;
225143 else
226144 ApplyThirdPersonRotation . Execute ( ref rigidTransform , in movementInput ) ;
227145
228- ApplyConditionalRotation . Execute ( ref rigidTransform , in settings ) ;
146+ if ( settings . EnableCharacterRotationBySlope )
147+ ApplySlopeConditionalRotation . Execute ( ref rigidTransform , in settings ) ;
229148 }
230149 }
231150}
0 commit comments