@@ -84,6 +84,7 @@ private void OnRespawn(Empty empty)
8484 if ( state == PlayerState . Dead )
8585 {
8686 anchorPoint = transform . position ;
87+ agent . Warp ( transform . position ) ;
8788 SetPlayerState ( PlayerState . LookingForTarget ) ;
8889 }
8990 }
@@ -140,13 +141,10 @@ private void Update_LookingForTarget()
140141 {
141142 if ( ! agent . isOnNavMesh )
142143 {
143- // If agent has fallen off the nav mesh, move forward and attempt to get back on the navmesh.
144- var velocity = transform . forward ;
145- var rotation = Quaternion . LookRotation ( velocity , Vector3 . up ) ;
146- var speed = sprintNext ? MovementSpeed . Sprint : MovementSpeed . Run ;
147- movementDriver . ApplyMovement ( velocity , rotation , speed , jumpNext ) ;
148- jumpNext = false ;
149- agent . Warp ( transform . position ) ;
144+ if ( NavMesh . SamplePosition ( transform . position , out var hit , 0.5f , NavMesh . AllAreas ) )
145+ {
146+ agent . Warp ( hit . position ) ;
147+ }
150148 }
151149 else if ( agent . remainingDistance < MinRemainingDistance || agent . pathStatus == NavMeshPathStatus . PathInvalid ||
152150 ! agent . hasPath )
@@ -161,7 +159,7 @@ private void Update_LookingForTarget()
161159 {
162160 var rotation = Quaternion . LookRotation ( velocity , Vector3 . up ) ;
163161 var speed = sprintNext ? MovementSpeed . Sprint : MovementSpeed . Run ;
164- movementDriver . ApplyMovement ( velocity , rotation , speed , jumpNext ) ;
162+ MoveTowards ( transform . position + velocity , speed , rotation , jumpNext ) ;
165163 jumpNext = false ;
166164 }
167165 }
@@ -180,8 +178,9 @@ private void Update_ShootingTarget()
180178 var targetRotation = Quaternion . LookRotation ( targetCenter - gunOrigin ) ;
181179 var rotationAmount = Quaternion . RotateTowards ( transform . rotation , targetRotation , 10f ) ;
182180
183- GetComponent < ClientMovementDriver > ( ) . ApplyMovement (
184- strafeRight ? transform . right : - transform . right , rotationAmount , MovementSpeed . Run , false ) ;
181+ var destination = transform . position + ( strafeRight ? transform . right : - transform . right ) ;
182+
183+ MoveTowards ( destination , MovementSpeed . Run , rotationAmount , false ) ;
185184
186185 if ( shooting . IsShooting ( true ) && Mathf . Abs ( Quaternion . Angle ( targetRotation , transform . rotation ) ) < 5 )
187186 {
@@ -203,6 +202,21 @@ private void Update_ShootingTarget()
203202 }
204203 }
205204
205+ private void MoveTowards ( Vector3 destination , MovementSpeed speed , Quaternion rotation , bool jump = false )
206+ {
207+ var agentPosition = agent . nextPosition ;
208+ var direction = ( destination - agentPosition ) . normalized ;
209+ var desiredVelocity = direction * movementDriver . GetSpeed ( speed ) ;
210+
211+ // Setting nextPosition will move the agent towards destination, but is constrained by the navmesh
212+ agent . nextPosition += desiredVelocity * Time . deltaTime ;
213+
214+ // Getting nextPosition here will return the constrained position of the agent
215+ var actualDirection = ( agent . nextPosition - agentPosition ) . normalized ;
216+
217+ movementDriver . ApplyMovement ( actualDirection , rotation , speed , jump ) ;
218+ }
219+
206220 private bool TargetIsValid ( )
207221 {
208222 if ( target == null )
@@ -316,7 +330,9 @@ private IEnumerator CheckImmobility()
316330 similarPositionsCount ++ ;
317331 if ( similarPositionsCount >= maxSimilarPositions )
318332 {
319- Debug . LogWarningFormat ( "{0} got stuck at {1}, respawning" , name , transform . position ) ;
333+ Debug . LogWarningFormat ( "{0} got stuck at {1}: agent at {2}, respawning" ,
334+ name , transform . position , agent . nextPosition ) ;
335+
320336 SetPlayerState ( PlayerState . Dead ) ;
321337 }
322338 }
0 commit comments