@@ -6,6 +6,8 @@ public class Humanoid : MonoBehaviour
66{
77 protected GroundCheck gndCheck ; // If grounded, incline checks, etc.
88 protected CharacterController controller ; // Movement handling.
9+ private HumanoidMoveStd stdMovement ; // Handles generall non-combat movement.
10+
911 public float Gravity = .032f ; // { get; set; } = .032f;
1012
1113 [ HideInInspector ] public bool StartedJmpCd { get ; private set ; } = false ; // Only allow one active jump cooldown.
@@ -17,19 +19,21 @@ public class Humanoid : MonoBehaviour
1719 public float jumpAmount = 0.35f , jumpCooldown = 0.8f ;
1820 public float accelMod = 10 ; // Used to calculate cur speed.
1921
22+
2023 private float _baseMaxSpd ;
2124
2225 void Awake ( )
2326 {
2427 gndCheck = GetComponent < GroundCheck > ( ) ;
2528 controller = GetComponent < CharacterController > ( ) ;
29+ stdMovement = GetComponent < HumanoidMoveStd > ( ) ;
2630 }
2731
2832
2933 // Update is called once per frame
3034 protected void Update ( )
3135 {
32- SetCurSpeed ( MaxSpd > 0 , CurSpd ) ;
36+ CurSpd = GetCurSpdAccel ( MaxSpd > 0 , CurSpd ) ;
3337 GravityCalc ( Gravity ) ;
3438
3539 if ( gndCheck . Grounded && ! CanJump ) // Start jump cooldown once grounded.
@@ -68,7 +72,7 @@ protected void Rotate(Vector3 newDir, int dampening, int rotSpeed)
6872
6973 private float CurVel => .5f * Vector3 . Magnitude
7074 ( new Vector3 ( controller . velocity . x , 0 , controller . velocity . z ) ) ; // Horizontal velocity.
71- private float GndSpdMod => ( .5f * ( gndCheck . GroundSlope * .4f ) ) * .15f ; // Terrain incline/decline speed modifier.
75+ private float GndSpdMod => ( .33f * ( gndCheck . GroundSlope * .4f ) ) * .15f ; // Terrain incline/decline speed modifier.
7276 private Vector3 moveVel = Vector3 . zero ; // Direction to move.
7377
7478 private float _minMvSpd = .2f ;
@@ -79,47 +83,25 @@ protected void Rotate(Vector3 newDir, int dampening, int rotSpeed)
7983 /// <param name="mxSpeed"></param>
8084 protected void Move ( float mxSpeed )
8185 {
82- _baseMaxSpd = mxSpeed ; // Save cur base max speed before modification.
83-
84- float newMax = 0 ;
85- // Max speed more/less depending on terrain incline.
86- if ( mxSpeed > 0 && controller . velocity . y >= 0 )
87- newMax = _minMvSpd + ( mxSpeed + CurVel ) - GndSpdMod ; // Less top speed if go up incline.
88- else if ( mxSpeed > 0 )
89- newMax = _minMvSpd + ( mxSpeed + CurVel ) + GndSpdMod ; // More top speed if down incline.
90-
91- // Clamp min/top max speed.
92- newMax = Mathf . Clamp ( newMax , 0 , mxSpeed * 1.6f ) ;
93-
94- // Update max speed if significant difference.
95- if ( Mathf . Abs ( _prevMaxSpd - newMax ) >= _minMvSpd )
96- {
97- if ( _baseMaxSpd > 0 && newMax < ( jogSpd * .8f ) ) // Enforce not too slow.
98- newMax = jogSpd * .8f ;
99-
100- _prevMaxSpd = newMax ;
101- MaxSpd = newMax ;
102- }
86+ // Update max speed for standard movement, and save original base max move speed for acceleration.
87+ MaxSpd = stdMovement . Move ( mxSpeed , jogSpd , _minMvSpd , CurVel , GndSpdMod , ref _prevMaxSpd ) ;
88+ _baseMaxSpd = mxSpeed ;
10389 }
10490
10591
10692 /// <summary>
107- /// Accel/Decel for current speed.
93+ /// Returns current speed after applying either Acceleration or Deceleration .
10894 /// </summary>
10995 /// <param name="moving"></param>
11096 /// <param name="curSpeed"></param>
11197 /// <param name="maxBaseSpd"></param>
11298 /// <returns></returns>
113- private void SetCurSpeed ( bool moving , float curSpeed )
99+ private float GetCurSpdAccel ( bool moving , float curSpeed )
114100 {
115101 // If in air, limit speed.
116- if ( ! gndCheck . Grounded )
117- {
118- CurSpd = .2f + ( Mathf . Clamp ( CurVel * .005f , 0 , .015f ) ) ;
119- return ;
120- }
121-
122- // Debug.Log("Max: " + MaxSpd + ", Cur: " + CurSpd) ;
102+ if ( ! gndCheck . Grounded )
103+ return .2f + ( Mathf . Clamp ( CurVel * .005f , 0 , .015f ) ) ;
104+
123105 float acclBonus = 0 ;
124106 // If base max spd < dynamic MaxSpd, then going down slope so increase max spd.
125107 if ( _baseMaxSpd <= MaxSpd )
@@ -139,7 +121,7 @@ private void SetCurSpeed(bool moving, float curSpeed)
139121 curSpeed = 0 ;
140122
141123 // Update applied current speed.
142- CurSpd = curSpeed ;
124+ return curSpeed ;
143125 }
144126
145127
0 commit comments