private void Pursue(Helper.ClosestResult target) { Vector3 targetPos = target.Character.transform.position; float t = Mathf.Sqrt(target.SqrDistance) / target.Character.Motor.maxSpeed; targetPos += t * target.Character.Motor.Rigidbody.velocity; Vector3 move = Motor.Arrive(targetPos).desiredVelocity; move += Separation(GameManager.Instance.EnemyGameObjects); move += Cohesion(GameManager.Instance.EnemyGameObjects); Motor.Move(move); }
private void FollowPlayer() { Character player = GameManager.Instance.Player; GameObject playerGameObject = player.gameObject; Vector3? target = player.CurrentTarget; Vector3 move; if (target.HasValue) { if (target == playerPreviousTarget && targets.Count > 0) { FollowPath(); return; } playerPreviousTarget = target; if (Physics.Linecast(transform.position, target.Value, GameManager.Instance.GridPlane.unwalkableMask)) { List <Node> nodes = GameManager.Instance.PathFinding.process(transform.position, target.Value); if (nodes == null) { move = Motor.Arrive(target.Value).desiredVelocity; } else { targets = new Queue <Vector3>(nodes.Select(node => node.getGridWorldPos())); FollowPath(); return; } } else { targets.Clear(); move = Motor.Arrive(target.Value).desiredVelocity; } } else { Vector3 cohesionPos = CohesionPosition(GameManager.Instance.FollowerGameObjects.Concat(new[] { playerGameObject })); if (((player.transform.position - cohesionPos).sqrMagnitude > sqrAllowedCohesionVariance) || Physics.Linecast(transform.position, player.transform.position, GameManager.Instance.GridPlane.unwalkableMask)) { if (player.transform.position == playerPreviousTarget && targets.Count > 0) { FollowPath(); return; } playerPreviousTarget = player.transform.position; List <Node> nodes = GameManager.Instance.PathFinding.process(transform.position, player.transform.position); if (nodes == null) { move = Motor.Arrive(player.transform.position).desiredVelocity; } else { targets = new Queue <Vector3>(nodes.Select(node => node.getGridWorldPos())); FollowPath(); return; } } else { move = Cohesion(cohesionPos); targets.Clear(); } } move += Separation(GameManager.Instance.FollowerGameObjects.Concat(new[] { playerGameObject })); Motor.Move(move); }