@@ -6,7 +6,7 @@ public class Humanoid : MonoBehaviour
66{
77 protected GroundCheck gndCheck ; // If grounded, incline checks, etc.
88 protected CharacterController controller ; // Movement handling.
9- protected float Gravity { get ; set ; } = .032f ;
9+ public float Gravity = .032f ; // { get; set; } = .032f;
1010
1111 [ HideInInspector ] public bool StartedJmpCd { get ; private set ; } = false ; // Only allow one active jump cooldown.
1212 [ HideInInspector ] public bool CanJump { get ; private set ; } = true ; // Jump cooldown lock.
@@ -17,6 +17,8 @@ public class Humanoid : MonoBehaviour
1717 public float jumpAmount = 0.35f , jumpCooldown = 0.8f ;
1818 public float accelMod = 10 ; // Used to calculate cur speed.
1919
20+ private float _baseMaxSpd ;
21+
2022 void Awake ( )
2123 {
2224 gndCheck = GetComponent < GroundCheck > ( ) ;
@@ -27,11 +29,13 @@ void Awake()
2729 // Update is called once per frame
2830 protected void Update ( )
2931 {
30- SetCurSpeed ( MaxSpd > 0 , CurSpd , MaxSpd ) ;
32+ SetCurSpeed ( MaxSpd > 0 , CurSpd ) ;
3133 GravityCalc ( Gravity ) ;
3234
3335 if ( gndCheck . Grounded && ! CanJump ) // Start jump cooldown once grounded.
3436 StartCoroutine ( JumpCooldown ( jumpCooldown ) ) ;
37+
38+ moveVel += ( transform . forward * CurSpd ) * Time . deltaTime ;
3539
3640 ApplyMoveVelocity ( ) ;
3741
@@ -67,29 +71,35 @@ protected void Rotate(Vector3 newDir, int dampening, int rotSpeed)
6771 private float GndSpdMod => ( .5f * ( gndCheck . GroundSlope * .4f ) ) * .15f ; // Terrain incline/decline speed modifier.
6872 private Vector3 moveVel = Vector3 . zero ; // Direction to move.
6973
70-
74+ private float _minMvSpd = .2f ;
75+ private float _prevMaxSpd = 0 ;
7176 /// <summary>
72- /// Apply move speed to horizontal move velocity .
77+ /// Sets new dynamic max speed to allow movement .
7378 /// </summary>
74- /// <param name="speed"></param>
75- protected void Move ( float speed )
76- {
79+ /// <param name="mxSpeed"></param>
80+ protected void Move ( float mxSpeed )
81+ {
82+ _baseMaxSpd = mxSpeed ; // Save cur base max speed before modification.
83+
84+ float newMax = 0 ;
7785 // Max speed more/less depending on terrain incline.
78- if ( controller . velocity . y >= 0 )
79- MaxSpd = ( speed + CurVel ) - GndSpdMod ; // Less top speed if go up incline.
80- else MaxSpd = ( speed + CurVel ) + GndSpdMod ; // More top speed if down 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.
8190
82- // Limit min/top max speed.
83- if ( speed != 0 )
84- {
85- if ( MaxSpd < 0 ) MaxSpd = 0 ;
86- else if ( MaxSpd > speed * 1.6f )
87- MaxSpd = speed * 1.6f ;
88- }
89-
91+ // Clamp min/top max speed.
92+ newMax = Mathf . Clamp ( newMax , 0 , mxSpeed * 1.6f ) ;
9093
91- if ( gndCheck . Grounded )
92- moveVel += ( transform . forward * CurSpd ) * Time . deltaTime ;
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+ }
93103 }
94104
95105
@@ -100,22 +110,35 @@ protected void Move(float speed)
100110 /// <param name="curSpeed"></param>
101111 /// <param name="maxBaseSpd"></param>
102112 /// <returns></returns>
103- private void SetCurSpeed ( bool moving , float curSpeed , float maxBaseSpd )
104- {
113+ private void SetCurSpeed ( bool moving , float curSpeed )
114+ {
115+ // 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) ;
105123 float acclBonus = 0 ;
106- if ( maxBaseSpd <= MaxSpd ) // Down slope - accel faster.
107- acclBonus += ( CurVel + GndSpdMod ) / 3 ;
108- else acclBonus -= ( CurVel + GndSpdMod ) / 3 ;
124+ // If base max spd < dynamic MaxSpd, then going down slope so increase max spd.
125+ if ( _baseMaxSpd <= MaxSpd )
126+ acclBonus += ( CurVel + GndSpdMod ) / 3 ; // Increase dynamic max speed (down slope).
127+ else acclBonus -= ( CurVel + GndSpdMod ) / 3 ; // Decrease it (up slope).
109128
110- if ( moving )
111- curSpeed += ( accelMod + acclBonus ) * Time . deltaTime ;
112- else curSpeed -= ( accelMod + acclBonus ) * Time . deltaTime ;
113-
114- // Limit min/max speed.
115- if ( curSpeed > MaxSpd )
129+ // Accel if slower than max, decel if too fast.
130+ if ( gndCheck . Grounded && moving && curSpeed < MaxSpd )
131+ curSpeed += ( accelMod + acclBonus ) * Time . deltaTime ;
132+ else if ( curSpeed > MaxSpd || ! gndCheck . Grounded )
133+ curSpeed -= ( accelMod + ( acclBonus * 1.5f ) ) * Time . deltaTime ;
134+
135+ // Round down to max speed if close enough.
136+ if ( Mathf . Abs ( curSpeed - MaxSpd ) < .2 && curSpeed > MaxSpd )
116137 curSpeed = MaxSpd ;
117138 else if ( curSpeed < 0 )
118139 curSpeed = 0 ;
140+
141+ // Update applied current speed.
119142 CurSpd = curSpeed ;
120143 }
121144
0 commit comments