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/Scenes/Main.unity | 4 ++-- Assets/Scripts/Controllers/AIController.cs | 23 +++++++++++++++++------ Assets/Scripts/Controllers/Ball.cs | 2 +- Assets/Sprites/Basketball.controller | 4 ++-- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/Assets/Scenes/Main.unity b/Assets/Scenes/Main.unity index a2bab54..1ead15b 100644 --- a/Assets/Scenes/Main.unity +++ b/Assets/Scenes/Main.unity @@ -712,11 +712,11 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 5143626018439341592, guid: 2485406c33a7d4f5481ce936cc9b162e, type: 3} propertyPath: m_LocalPosition.x - value: 0.23 + value: -0.00999999 objectReference: {fileID: 0} - target: {fileID: 5143626018439341592, guid: 2485406c33a7d4f5481ce936cc9b162e, type: 3} propertyPath: m_LocalPosition.y - value: 5.17 + value: 0.73 objectReference: {fileID: 0} - target: {fileID: 5143626018439341592, guid: 2485406c33a7d4f5481ce936cc9b162e, type: 3} propertyPath: m_LocalPosition.z 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); } diff --git a/Assets/Sprites/Basketball.controller b/Assets/Sprites/Basketball.controller index 0b17d66..51c508c 100644 --- a/Assets/Sprites/Basketball.controller +++ b/Assets/Sprites/Basketball.controller @@ -11,13 +11,13 @@ AnimatorStateMachine: m_ChildStates: - serializedVersion: 1 m_State: {fileID: 7946675803598422961} - m_Position: {x: 200, y: 0, z: 0} + m_Position: {x: 260, y: 60, z: 0} m_ChildStateMachines: [] m_AnyStateTransitions: [] m_EntryTransitions: [] m_StateMachineTransitions: {} m_StateMachineBehaviours: [] - m_AnyStatePosition: {x: 50, y: 20, z: 0} + m_AnyStatePosition: {x: 60, y: 10, z: 0} m_EntryPosition: {x: 50, y: 120, z: 0} m_ExitPosition: {x: 800, y: 120, z: 0} m_ParentStateMachinePosition: {x: 800, y: 20, z: 0} -- cgit v1.2.3-56-ge451