From 71e603101369cf4a144904a2fc0e45ad3f862296 Mon Sep 17 00:00:00 2001 From: Carson Katri Date: Sat, 12 Jun 2021 17:31:33 -0400 Subject: [PATCH] Refine enemy locomotion --- Assets/Prefabs/Enemy.prefab | 19 ++-- Assets/Scenes/Main.unity | 88 ++++++++++++++++++- Assets/Scripts/Controllers/AIController.cs | 46 +++++----- Assets/Scripts/Controllers/GameController.cs | 10 ++- .../Controllers/Player/PlayerController.cs | 2 + ProjectSettings/Physics2DSettings.asset | 2 +- ProjectSettings/TagManager.asset | 2 +- 7 files changed, 129 insertions(+), 40 deletions(-) diff --git a/Assets/Prefabs/Enemy.prefab b/Assets/Prefabs/Enemy.prefab index 6344eab..14429da 100644 --- a/Assets/Prefabs/Enemy.prefab +++ b/Assets/Prefabs/Enemy.prefab @@ -10,7 +10,7 @@ GameObject: m_Component: - component: {fileID: 7079067998564477638} - component: {fileID: 7079067998564477637} - m_Layer: 6 + m_Layer: 8 m_Name: Leg.Right.Top m_TagString: Untagged m_Icon: {fileID: 0} @@ -93,7 +93,7 @@ GameObject: m_Component: - component: {fileID: 7079067998874270570} - component: {fileID: 7079067998874270569} - m_Layer: 6 + m_Layer: 8 m_Name: Arm.Left m_TagString: Untagged m_Icon: {fileID: 0} @@ -175,7 +175,7 @@ GameObject: m_Component: - component: {fileID: 7079067998927418495} - component: {fileID: 7079067998927418494} - m_Layer: 6 + m_Layer: 8 m_Name: Leg.Left.Bottom m_TagString: Untagged m_Icon: {fileID: 0} @@ -257,7 +257,7 @@ GameObject: m_Component: - component: {fileID: 7079067998994176362} - component: {fileID: 7079067998994176361} - m_Layer: 6 + m_Layer: 8 m_Name: Left.Left.Top m_TagString: Untagged m_Icon: {fileID: 0} @@ -342,7 +342,7 @@ GameObject: - component: {fileID: 670472073231852718} - component: {fileID: 391441287063834562} - component: {fileID: 2915148967131225220} - m_Layer: 6 + m_Layer: 8 m_Name: Enemy m_TagString: Untagged m_Icon: {fileID: 0} @@ -391,6 +391,7 @@ MonoBehaviour: maxLegAngle: 30 legSpeed: 20 game: {fileID: 0} + player: {fileID: 0} --- !u!50 &391441287063834562 Rigidbody2D: serializedVersion: 4 @@ -448,7 +449,7 @@ GameObject: m_Component: - component: {fileID: 7079067999404952341} - component: {fileID: 7079067999404952340} - m_Layer: 6 + m_Layer: 8 m_Name: Arm.Right m_TagString: Untagged m_Icon: {fileID: 0} @@ -530,7 +531,7 @@ GameObject: m_Component: - component: {fileID: 7079067999656997561} - component: {fileID: 7079067999656997560} - m_Layer: 6 + m_Layer: 8 m_Name: Head m_TagString: Untagged m_Icon: {fileID: 0} @@ -612,7 +613,7 @@ GameObject: m_Component: - component: {fileID: 7079068000117515304} - component: {fileID: 7079068000117515303} - m_Layer: 6 + m_Layer: 8 m_Name: Body m_TagString: Untagged m_Icon: {fileID: 0} @@ -694,7 +695,7 @@ GameObject: m_Component: - component: {fileID: 7079068000273710487} - component: {fileID: 7079068000273710486} - m_Layer: 6 + m_Layer: 8 m_Name: Leg.Right.Bottom m_TagString: Untagged m_Icon: {fileID: 0} diff --git a/Assets/Scenes/Main.unity b/Assets/Scenes/Main.unity index 0871fd5..769348d 100644 --- a/Assets/Scenes/Main.unity +++ b/Assets/Scenes/Main.unity @@ -152,6 +152,7 @@ Transform: m_Children: - {fileID: 1174357919} - {fileID: 1189511746} + - {fileID: 961513381} m_Father: {fileID: 0} m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} @@ -366,7 +367,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 6267610020350316690, guid: 2485406c33a7d4f5481ce936cc9b162e, type: 3} propertyPath: shotForce - value: 1000 + value: 500 objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 2485406c33a7d4f5481ce936cc9b162e, type: 3} @@ -381,6 +382,80 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 71c18c0662ec4384bf64aae1dc016093, type: 3} m_Name: m_EditorClassIdentifier: +--- !u!1001 &961513380 +PrefabInstance: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_Modification: + m_TransformParent: {fileID: 244382786} + m_Modifications: + - target: {fileID: 670472073231852718, guid: d9b5414ac39834728992e2585c5ad778, type: 3} + propertyPath: game + value: + objectReference: {fileID: 1174357920} + - target: {fileID: 670472073231852718, guid: d9b5414ac39834728992e2585c5ad778, type: 3} + propertyPath: player + value: + objectReference: {fileID: 984947993} + - target: {fileID: 670472073231852718, guid: d9b5414ac39834728992e2585c5ad778, type: 3} + propertyPath: legSpeed + value: 5 + objectReference: {fileID: 0} + - target: {fileID: 7079067999241254396, guid: d9b5414ac39834728992e2585c5ad778, type: 3} + propertyPath: m_RootOrder + value: 2 + objectReference: {fileID: 0} + - target: {fileID: 7079067999241254396, guid: d9b5414ac39834728992e2585c5ad778, type: 3} + propertyPath: m_LocalPosition.x + value: 1.651358 + objectReference: {fileID: 0} + - target: {fileID: 7079067999241254396, guid: d9b5414ac39834728992e2585c5ad778, type: 3} + propertyPath: m_LocalPosition.y + value: -2.7138848 + objectReference: {fileID: 0} + - target: {fileID: 7079067999241254396, guid: d9b5414ac39834728992e2585c5ad778, type: 3} + propertyPath: m_LocalPosition.z + value: 134.58575 + objectReference: {fileID: 0} + - target: {fileID: 7079067999241254396, guid: d9b5414ac39834728992e2585c5ad778, type: 3} + propertyPath: m_LocalRotation.w + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 7079067999241254396, guid: d9b5414ac39834728992e2585c5ad778, type: 3} + propertyPath: m_LocalRotation.x + value: -0 + objectReference: {fileID: 0} + - target: {fileID: 7079067999241254396, guid: d9b5414ac39834728992e2585c5ad778, type: 3} + propertyPath: m_LocalRotation.y + value: -0 + objectReference: {fileID: 0} + - target: {fileID: 7079067999241254396, guid: d9b5414ac39834728992e2585c5ad778, type: 3} + propertyPath: m_LocalRotation.z + value: -0 + objectReference: {fileID: 0} + - target: {fileID: 7079067999241254396, guid: d9b5414ac39834728992e2585c5ad778, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7079067999241254396, guid: d9b5414ac39834728992e2585c5ad778, type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7079067999241254396, guid: d9b5414ac39834728992e2585c5ad778, type: 3} + propertyPath: m_LocalEulerAnglesHint.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 7079067999241254397, guid: d9b5414ac39834728992e2585c5ad778, type: 3} + propertyPath: m_Name + value: Enemy + objectReference: {fileID: 0} + m_RemovedComponents: [] + m_SourcePrefab: {fileID: 100100000, guid: d9b5414ac39834728992e2585c5ad778, type: 3} +--- !u!4 &961513381 stripped +Transform: + m_CorrespondingSourceObject: {fileID: 7079067999241254396, guid: d9b5414ac39834728992e2585c5ad778, type: 3} + m_PrefabInstance: {fileID: 961513380} + m_PrefabAsset: {fileID: 0} --- !u!1001 &984947992 PrefabInstance: m_ObjectHideFlags: 0 @@ -458,6 +533,17 @@ PrefabInstance: objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: c64ad9b908cb44602b3d13a74aef751f, type: 3} +--- !u!114 &984947993 stripped +MonoBehaviour: + m_CorrespondingSourceObject: {fileID: 6095859834264611326, guid: c64ad9b908cb44602b3d13a74aef751f, type: 3} + m_PrefabInstance: {fileID: 984947992} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 261cc3206075846ebbd03b8896e7c733, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!4 &1116995016 stripped Transform: m_CorrespondingSourceObject: {fileID: 2106494524751008833, guid: b2939f2eae52d074b81e81fccdde88f9, type: 3} 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) { diff --git a/ProjectSettings/Physics2DSettings.asset b/ProjectSettings/Physics2DSettings.asset index e6f20a9..4cfea27 100644 --- a/ProjectSettings/Physics2DSettings.asset +++ b/ProjectSettings/Physics2DSettings.asset @@ -53,4 +53,4 @@ Physics2DSettings: m_ColliderAsleepColor: {r: 0.5686275, g: 0.95686275, b: 0.54509807, a: 0.36078432} m_ColliderContactColor: {r: 1, g: 0, b: 1, a: 0.6862745} m_ColliderAABBColor: {r: 1, g: 1, b: 0, a: 0.2509804} - m_LayerCollisionMatrix: ffffffffffffffffffffffffffffffffffffffffffffffff7fffffffbfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + m_LayerCollisionMatrix: ffffffffffffffffffffffffffffffffffffffffffffffff7ffeffffbffeffff3fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff diff --git a/ProjectSettings/TagManager.asset b/ProjectSettings/TagManager.asset index 9793b29..3040a17 100644 --- a/ProjectSettings/TagManager.asset +++ b/ProjectSettings/TagManager.asset @@ -13,7 +13,7 @@ TagManager: - UI - Player - Ball - - + - Enemy - - - -- 2.47.1