From 585e0ebbd768eb3955c3bad19ef425bdf6657f8f Mon Sep 17 00:00:00 2001 From: Carson Katri Date: Sat, 12 Jun 2021 20:19:00 -0400 Subject: Refine AI decision-making --- Assets/Scripts/Controllers/AIController.cs | 23 +++++++++++++++++------ Assets/Scripts/Controllers/Ball.cs | 2 +- 2 files changed, 18 insertions(+), 7 deletions(-) (limited to 'Assets/Scripts') diff --git a/Assets/Scripts/Controllers/AIController.cs b/Assets/Scripts/Controllers/AIController.cs index 244b502..4879c4c 100644 --- a/Assets/Scripts/Controllers/AIController.cs +++ b/Assets/Scripts/Controllers/AIController.cs @@ -35,6 +35,7 @@ namespace Controllers { // Try to grab (steal) the ball every n seconds. InvokeRepeating(nameof(GrabBall), 0, 0.5f); + InvokeRepeating(nameof(ShootBall), 0, 2f); } private void GrabBall() @@ -47,6 +48,17 @@ namespace Controllers } } + private void ShootBall() + { + if (!game.enemy.HasBall) return; + var hoopDistance = Mathf.Abs(transform.position.x - game.EnemyHoop.transform.position.x); + var playerHoopDistance = Mathf.Abs(player.root.transform.position.x - game.EnemyHoop.transform.position.x); + if (hoopDistance < 6f && hoopDistance > 2.5f && (Random.Range(0, 100) > 30 || playerHoopDistance > hoopDistance)) // Take the shot when in range. 70% chance he decides to if the player is standing between him and the basket. + { + game.enemy.Shoot(transform); + } + } + private void Update() { var movement = transform.position; @@ -68,16 +80,15 @@ namespace Controllers } else // Otherwise, move toward the basket, and then once we get within range, take the shot. { - transform.position = new Vector2(Vector2.MoveTowards(transform.position, game.EnemyHoop.transform.position, speed * Time.deltaTime).x, transform.position.y); - if (Mathf.Abs(transform.position.x - game.EnemyHoop.transform.position.x) < 5f) // Take the shot. - { - game.enemy.Shoot(transform); - } + // How far the enemy tries to get from the hoop before shooting. + // Currently, it will try to shoot a 3pt. shot if down, but a 2pt. shot if up or tied. + var targetDistance = game.enemy.score < game.player.score ? 5f : 3f; + transform.position = new Vector2(Vector2.MoveTowards(transform.position, game.EnemyHoop.transform.position + new Vector3(targetDistance, 0, 0), speed * Time.deltaTime).x, transform.position.y); } movement -= transform.position; - game.enemy.Move(transform.position + new Vector3(0f, BoxCollider.size.y / 2, 0f)); + game.enemy.Move(transform.position + new Vector3(0f, BoxCollider.size.y * 3/4, 0f)); leftLeg.top.transform.localRotation = Quaternion.Euler(0, 0, maxLegAngle * Mathf.Sin(animationStep * legSpeed)); leftLeg.bottom.transform.localRotation = Quaternion.Euler(0, 0, maxLegAngle + maxLegAngle * Mathf.Sin(animationStep * legSpeed)); diff --git a/Assets/Scripts/Controllers/Ball.cs b/Assets/Scripts/Controllers/Ball.cs index d9c85c2..07e1b37 100644 --- a/Assets/Scripts/Controllers/Ball.cs +++ b/Assets/Scripts/Controllers/Ball.cs @@ -22,7 +22,7 @@ namespace Controllers public void Shoot(Vector3 target) { - transform.right = (target - transform.position); + transform.right = target - transform.position; Rigidbody.velocity = Vector2.zero; Rigidbody.AddForce((transform.right + (transform.up * 0.5f)) * shotForce); } -- cgit v1.2.3-56-ge451