Skip to content
This repository was archived by the owner on Oct 20, 2021. It is now read-only.

Commit 9dd1b7f

Browse files
Bugfix/AI navigation (#46)
* Make ClientMovementDriver.GetSpeed a public method * Create new method MoveTowards in SimulatedPlayerDriver that sets/gets the agent.nextPosition to help constrain movement to the navmesh
1 parent 9e2db54 commit 9dd1b7f

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

workers/unity/Assets/Fps/Scripts/SimulatedPlayer/SimulatedPlayerDriver.cs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

workers/unity/Packages/com.improbable.gdk.movement/Behaviours/ClientMovementDriver.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ private void OnForcedRotation(RotationUpdate forcedRotation)
165165

166166
cumulativeRotationTimeDelta = 0;
167167
pitchDirty = rollDirty = yawDirty = false;
168-
168+
169169
//Apply the forced rotation
170170
var x = rotationConstraints.XAxisRotation ? forcedRotation.Pitch.ToFloat1k() : 0;
171171
var y = rotationConstraints.YAxisRotation ? forcedRotation.Yaw.ToFloat1k() : 0;
@@ -280,7 +280,7 @@ public void ProcessInput(Vector3 movement, Quaternion rotation, MovementSpeed mo
280280
CurrentRoll = rotation.eulerAngles.z;
281281
}
282282

283-
private float GetSpeed(MovementSpeed requestedSpeed)
283+
public float GetSpeed(MovementSpeed requestedSpeed)
284284
{
285285
switch (requestedSpeed)
286286
{

0 commit comments

Comments
 (0)