From 71e603101369cf4a144904a2fc0e45ad3f862296 Mon Sep 17 00:00:00 2001 From: Carson Katri Date: Sat, 12 Jun 2021 17:31:33 -0400 Subject: Refine enemy locomotion --- Assets/Scripts/Controllers/AIController.cs | 46 ++++++++++------------ Assets/Scripts/Controllers/GameController.cs | 10 +++-- .../Scripts/Controllers/Player/PlayerController.cs | 2 + 3 files changed, 30 insertions(+), 28 deletions(-) (limited to 'Assets/Scripts') diff --git a/Assets/Scripts/Controllers/AIController.cs b/Assets/Scripts/Controllers/AIController.cs index 1b7fba8..0edffb7 100644 --- a/Assets/Scripts/Controllers/AIController.cs +++ b/Assets/Scripts/Controllers/AIController.cs @@ -23,6 +23,8 @@ namespace Controllers public Rigidbody2D Rigidbody => _rigidbody != null ? _rigidbody : _rigidbody = GetComponent(); private Rigidbody2D _rigidbody; + + private float animationStep; public BoxCollider2D BoxCollider => _boxCollider != null ? _boxCollider : _boxCollider = GetComponent(); private BoxCollider2D _boxCollider; @@ -36,7 +38,7 @@ namespace Controllers private void GrabBall() { // Grab from the middle, and the bottom. - if (game.player.HasBall && Random.Range(0, 100) > 95) // 5% chance of them grabbing the ball from the player. + if (!game.player.HasBall || Random.Range(0, 100) > 70) // 30% chance of them grabbing the ball from the player. { game.enemy.GrabBall(transform.position); game.enemy.GrabBall(transform.position + new Vector3(0f, BoxCollider.size.y / 2, 0f)); @@ -45,41 +47,35 @@ namespace Controllers private void Update() { + var movement = transform.position; + if (!game.enemy.HasBall) // Move towards the ball to grab it. { - transform.position += new Vector3(game.ball.transform.position.x - transform.position.x, 0f, 0f).normalized * (Time.deltaTime * speed); - - if (Vector2.Distance(player.transform.position, transform.position) < 1f) // Repel from the player if they get too close. + if (Vector2.Distance(player.root.transform.position, transform.position) < 2f) // Repel from the player if they get too close. + { + // transform.position += new Vector3(-(player.transform.position - transform.position).normalized.x, 0, 0) * (Time.deltaTime * speed / 2f); + transform.position = new Vector2(Vector2.MoveTowards(transform.position, player.root.transform.position, -1.5f * speed * Time.deltaTime).x, transform.position.y); + } + else { - transform.position += (player.transform.position - transform.position).normalized * (Time.deltaTime * speed / 2f); + // transform.position += new Vector3(game.ball.transform.position.x - transform.position.x, 0f, 0f).normalized * (Time.deltaTime * speed); + transform.position = new Vector2(Vector2.MoveTowards(transform.position, game.ball.transform.position, speed * Time.deltaTime).x, transform.position.y); } } else // Otherwise, move toward the basket, and then once we get within range, take the shot. { } + movement -= transform.position; + game.enemy.Move(transform.position + new Vector3(0f, BoxCollider.size.y / 2, 0f)); - leftLeg.top.transform.localRotation = Quaternion.Slerp( - leftLeg.top.transform.localRotation, - Quaternion.Euler(0, 0, maxLegAngle * Mathf.Sin(Time.time * legSpeed * (Rigidbody.velocity.magnitude / 5f))), - Time.deltaTime * 50f - ); - leftLeg.bottom.transform.localRotation = Quaternion.Slerp( - leftLeg.bottom.transform.localRotation, - Quaternion.Euler(0, 0, maxLegAngle + maxLegAngle * Mathf.Sin(Time.time * legSpeed * (Rigidbody.velocity.magnitude / 5f))), - Time.deltaTime * 50f - ); - rightLeg.top.transform.localRotation = Quaternion.Slerp( - rightLeg.top.transform.localRotation, - Quaternion.Euler(0, 0, maxLegAngle * Mathf.Sin(Time.time * -legSpeed * (Rigidbody.velocity.magnitude / 5f))), - Time.deltaTime * 50f - ); - rightLeg.bottom.transform.localRotation = Quaternion.Slerp( - rightLeg.bottom.transform.localRotation, - Quaternion.Euler(0, 0, maxLegAngle + maxLegAngle * Mathf.Sin(Time.time * -legSpeed * (Rigidbody.velocity.magnitude / 5f))), - Time.deltaTime * 50f - ); + 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)); + rightLeg.top.transform.localRotation = Quaternion.Euler(0, 0, maxLegAngle * Mathf.Sin(animationStep * -legSpeed)); + rightLeg.bottom.transform.localRotation = Quaternion.Euler(0, 0, maxLegAngle + maxLegAngle * Mathf.Sin(animationStep * -legSpeed)); + + animationStep += Time.deltaTime * Mathf.Abs(movement.x); } [Serializable] diff --git a/Assets/Scripts/Controllers/GameController.cs b/Assets/Scripts/Controllers/GameController.cs index b9f65f2..646b750 100644 --- a/Assets/Scripts/Controllers/GameController.cs +++ b/Assets/Scripts/Controllers/GameController.cs @@ -43,14 +43,18 @@ namespace Controllers internal int score; private Vector2 lastShotPosition; - public void Score(Vector2 Rim) { + public void Score(Vector2 Rim) + { if (Vector2.Distance(lastShotPosition, Rim) >= 1) { score += 3; Debug.Log("Three point"); - } else + } + else + { score += 2; Debug.Log("Two point"); + } } private State dribble => isEnemy ? State.EnemyDribble : State.PlayerDribble; @@ -76,7 +80,7 @@ namespace Controllers public bool GrabBall(Vector2 handPosition) { // Don't allow the ball to be picked up if someone shot it. Also don't try picking it up if we're already holding it. - if (controller.state.IsShot() || controller.state == dribble) return false; + //if (controller.state.IsShot() || controller.state == dribble) return false; // Make sure its within their grab area. if (Vector2.Distance(controller.ball.transform.position, handPosition) > 0.75f) return false; diff --git a/Assets/Scripts/Controllers/Player/PlayerController.cs b/Assets/Scripts/Controllers/Player/PlayerController.cs index e67358c..6436235 100644 --- a/Assets/Scripts/Controllers/Player/PlayerController.cs +++ b/Assets/Scripts/Controllers/Player/PlayerController.cs @@ -18,6 +18,8 @@ namespace Controllers.Player [SerializeField] private float maxLegAngle; [SerializeField] private float legSpeed; + public PlayerSegment root => segments[0]; + private void Awake() { foreach (var segment in segments) { -- cgit v1.2.3-56-ge451