From 49360ba4e83f6282450c09160143caf7e12086a1 Mon Sep 17 00:00:00 2001 From: Sveske Juice Date: Sat, 3 Feb 2024 05:07:03 -0800 Subject: [PATCH] basic rope whip attack --- Assets/Resources/Player1.prefab | 25 +++- Assets/Scenes/EnemyRope.unity | 124 ++++++++++++++---- .../{PlayerMovement1.cs => PlayerMovement.cs} | 35 ++++- ...vement1.cs.meta => PlayerMovement.cs.meta} | 0 Assets/Scripts/Player/PlayerMovement2.cs | 75 ----------- Assets/Scripts/Player/RopeWhipAttack.cs | 38 ++++++ ...vement2.cs.meta => RopeWhipAttack.cs.meta} | 2 +- Assets/Scripts/Rope/RopeSimulator.cs | 9 +- 8 files changed, 203 insertions(+), 105 deletions(-) rename Assets/Scripts/Player/{PlayerMovement1.cs => PlayerMovement.cs} (64%) rename Assets/Scripts/Player/{PlayerMovement1.cs.meta => PlayerMovement.cs.meta} (100%) delete mode 100644 Assets/Scripts/Player/PlayerMovement2.cs create mode 100644 Assets/Scripts/Player/RopeWhipAttack.cs rename Assets/Scripts/Player/{PlayerMovement2.cs.meta => RopeWhipAttack.cs.meta} (83%) diff --git a/Assets/Resources/Player1.prefab b/Assets/Resources/Player1.prefab index 0bfdcb7..fc4a398 100644 --- a/Assets/Resources/Player1.prefab +++ b/Assets/Resources/Player1.prefab @@ -187,6 +187,7 @@ GameObject: - component: {fileID: 1508323898269695585} - component: {fileID: 5467488512035376674} - component: {fileID: 3878447480781341932} + - component: {fileID: 1749848915408613053} m_Layer: 7 m_Name: Player1 m_TagString: Player @@ -223,10 +224,14 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 3dbdb849a2f5df14d9e109a7776c5ac0, type: 3} m_Name: m_EditorClassIdentifier: - moveSpeed: 5 + player: 0 + moveSpeed: 70 stepCooldown: 0.2 stepVibrationTime: 0.05 rumble: {fileID: 0} + whipAttack: {fileID: 1749848915408613053} + whipMoveSpeed: 25 + maxWhipMoveSpeed: 30 --- !u!114 &3086165646112058191 MonoBehaviour: m_ObjectHideFlags: 0 @@ -261,7 +266,7 @@ Rigidbody2D: m_UseFullKinematicContacts: 0 m_UseAutoMass: 0 m_Mass: 1 - m_LinearDrag: 0 + m_LinearDrag: 10 m_AngularDrag: 0.05 m_GravityScale: 0 m_Material: {fileID: 6200000, guid: 7c680ffb793a1f2478e6682c957e4868, type: 2} @@ -302,3 +307,19 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 9c2ddf673a65d3d49bc9470e1a4572e4, type: 3} m_Name: m_EditorClassIdentifier: +--- !u!114 &1749848915408613053 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 3120938410244321186} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: b69b92d72c7244443854899c4b700d9c, type: 3} + m_Name: + m_EditorClassIdentifier: + otherPlayerAttack: {fileID: 0} + playerMovement: {fileID: 651446758998956252} + joint: {fileID: 5467488512035376674} + initialDrag: 0 diff --git a/Assets/Scenes/EnemyRope.unity b/Assets/Scenes/EnemyRope.unity index 6c5f4fd..2f55bd1 100644 --- a/Assets/Scenes/EnemyRope.unity +++ b/Assets/Scenes/EnemyRope.unity @@ -134,6 +134,7 @@ GameObject: - component: {fileID: 63829549} - component: {fileID: 63829548} - component: {fileID: 63829547} + - component: {fileID: 63829550} m_Layer: 0 m_Name: Main Camera m_TagString: MainCamera @@ -184,7 +185,7 @@ Camera: far clip plane: 1000 field of view: 60 orthographic: 1 - orthographic size: 5 + orthographic size: 16.46 m_Depth: -1 m_CullingMask: serializedVersion: 2 @@ -215,6 +216,50 @@ Transform: m_Children: [] m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &63829550 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 63829546} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: a79441f348de89743a2939f4d699eac1, type: 3} + m_Name: + m_EditorClassIdentifier: + m_RenderShadows: 1 + m_RequiresDepthTextureOption: 2 + m_RequiresOpaqueTextureOption: 2 + m_CameraType: 0 + m_Cameras: [] + m_RendererIndex: -1 + m_VolumeLayerMask: + serializedVersion: 2 + m_Bits: 1 + m_VolumeTrigger: {fileID: 0} + m_VolumeFrameworkUpdateModeOption: 2 + m_RenderPostProcessing: 0 + m_Antialiasing: 0 + m_AntialiasingQuality: 2 + m_StopNaN: 0 + m_Dithering: 0 + m_ClearDepth: 1 + m_AllowXRRendering: 1 + m_AllowHDROutput: 1 + m_UseScreenCoordOverride: 0 + m_ScreenSizeOverride: {x: 0, y: 0, z: 0, w: 0} + m_ScreenCoordScaleBias: {x: 0, y: 0, z: 0, w: 0} + m_RequiresDepthTexture: 0 + m_RequiresColorTexture: 0 + m_Version: 2 + m_TaaSettings: + quality: 3 + frameInfluence: 0.1 + jitterScale: 1 + mipBias: 0 + varianceClampScale: 0.9 + contrastAdaptiveSharpening: 0 --- !u!1 &99394924 GameObject: m_ObjectHideFlags: 0 @@ -678,6 +723,17 @@ MonoBehaviour: serializedVersion: 2 m_Bits: 128 inRange: 0 +--- !u!114 &465041015 stripped +MonoBehaviour: + m_CorrespondingSourceObject: {fileID: 1749848915408613053, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} + m_PrefabInstance: {fileID: 1273044612} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: b69b92d72c7244443854899c4b700d9c, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &625885767 GameObject: m_ObjectHideFlags: 0 @@ -1391,38 +1447,28 @@ MonoBehaviour: m_Area: 1 m_IgnoreFromBuild: 0 m_AffectedAgents: ffffffff ---- !u!1 &1259212307 stripped -GameObject: - m_CorrespondingSourceObject: {fileID: 3120938410244321186, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} - m_PrefabInstance: {fileID: 1844950416} - m_PrefabAsset: {fileID: 0} --- !u!114 &1259212308 stripped MonoBehaviour: m_CorrespondingSourceObject: {fileID: 5467488512035376674, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} m_PrefabInstance: {fileID: 1844950416} m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1259212307} + m_GameObject: {fileID: 0} m_Enabled: 1 m_EditorHideFlags: 0 m_Script: {fileID: 11500000, guid: 4aabce12c1341ff488ee4dc806fd80e8, type: 3} m_Name: m_EditorClassIdentifier: ---- !u!114 &1259212311 +--- !u!114 &1259212316 stripped MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} + m_CorrespondingSourceObject: {fileID: 1749848915408613053, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} + m_PrefabInstance: {fileID: 1844950416} m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1259212307} + m_GameObject: {fileID: 0} m_Enabled: 1 m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: e891644cda7544948af35b0d2f2c3be2, type: 3} + m_Script: {fileID: 11500000, guid: b69b92d72c7244443854899c4b700d9c, type: 3} m_Name: m_EditorClassIdentifier: - moveSpeed: 5 - stepCooldown: 0.05 - stepVibrationTime: 0.05 - rumble: {fileID: 1744797149} --- !u!1001 &1273044612 PrefabInstance: m_ObjectHideFlags: 0 @@ -1475,10 +1521,26 @@ PrefabInstance: propertyPath: rumble value: objectReference: {fileID: 1744797149} + - target: {fileID: 1749848915408613053, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} + propertyPath: otherPlayerAttack + value: + objectReference: {fileID: 1259212316} - target: {fileID: 3120938410244321186, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} propertyPath: m_Name value: Player 1 objectReference: {fileID: 0} + - target: {fileID: 3876439528830135059, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} + propertyPath: otherPlayerAttack + value: + objectReference: {fileID: 0} + - target: {fileID: 5882735032023506905, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} + propertyPath: otherPlayerAttack + value: + objectReference: {fileID: 0} + - target: {fileID: 7667011999606814046, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} + propertyPath: otherPlayerAttack + value: + objectReference: {fileID: 0} m_RemovedComponents: [] m_RemovedGameObjects: [] m_AddedGameObjects: [] @@ -2221,22 +2283,38 @@ PrefabInstance: propertyPath: m_LocalEulerAnglesHint.z value: 0 objectReference: {fileID: 0} + - target: {fileID: 651446758998956252, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} + propertyPath: player + value: 1 + objectReference: {fileID: 0} - target: {fileID: 651446758998956252, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} propertyPath: rumble value: objectReference: {fileID: 1744797149} + - target: {fileID: 1749848915408613053, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} + propertyPath: otherPlayerAttack + value: + objectReference: {fileID: 465041015} - target: {fileID: 3120938410244321186, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} propertyPath: m_Name value: Player 2 objectReference: {fileID: 0} - m_RemovedComponents: - - {fileID: 651446758998956252, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} + - target: {fileID: 3876439528830135059, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} + propertyPath: otherPlayerAttack + value: + objectReference: {fileID: 0} + - target: {fileID: 5882735032023506905, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} + propertyPath: otherPlayerAttack + value: + objectReference: {fileID: 0} + - target: {fileID: 7667011999606814046, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} + propertyPath: otherPlayerAttack + value: + objectReference: {fileID: 0} + m_RemovedComponents: [] m_RemovedGameObjects: [] m_AddedGameObjects: [] - m_AddedComponents: - - targetCorrespondingSourceObject: {fileID: 3120938410244321186, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} - insertIndex: -1 - addedObject: {fileID: 1259212311} + m_AddedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 99a6ff8b9591949439b620b13bd249a4, type: 3} --- !u!1660057539 &9223372036854775807 SceneRoots: diff --git a/Assets/Scripts/Player/PlayerMovement1.cs b/Assets/Scripts/Player/PlayerMovement.cs similarity index 64% rename from Assets/Scripts/Player/PlayerMovement1.cs rename to Assets/Scripts/Player/PlayerMovement.cs index a888321..d9226cc 100644 --- a/Assets/Scripts/Player/PlayerMovement1.cs +++ b/Assets/Scripts/Player/PlayerMovement.cs @@ -3,8 +3,9 @@ using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; -public class PlayerMovement1 : MonoBehaviour +public class PlayerMovement : MonoBehaviour { + public int player = 0; public float moveSpeed = 5f; private Rigidbody2D rb; private Vector2 movement; @@ -18,11 +19,21 @@ public class PlayerMovement1 : MonoBehaviour [SerializeField] private GameObject rumble; + [Header("Whipping")] + [SerializeField] + RopeWhipAttack whipAttack; + + [SerializeField] + private float whipMoveSpeed = 25f; + + [SerializeField] + private float maxWhipMoveSpeed = 30f; + private void Start() { rb = GetComponent(); - playerController = Gamepad.all[0]; + playerController = Gamepad.all[player]; StartCoroutine(ToggleWithDelay()); } @@ -44,7 +55,25 @@ public class PlayerMovement1 : MonoBehaviour } private void FixedUpdate() { - rb.velocity = (movement * moveSpeed); + if (whipAttack.IsBeingWhipped) + { + float sign = movement.x > 0 ? 1 : -1; + if (movement.x == 0) + sign = 0; + Vector2 ropeDir = whipAttack.otherPlayerAttack.joint.position - whipAttack.joint.position; + Vector2 tangent = new Vector2(-ropeDir.y, ropeDir.x).normalized; + rb.AddForce(tangent * whipMoveSpeed * sign); + rb.velocity = Vector2.ClampMagnitude(rb.velocity, maxWhipMoveSpeed); + } + else if (whipAttack.IsWhippingOtherPlayer) + { + movement = Vector2.zero; + } + else + { + rb.AddForce(movement * moveSpeed); + } + } private void RumbleWalk() { diff --git a/Assets/Scripts/Player/PlayerMovement1.cs.meta b/Assets/Scripts/Player/PlayerMovement.cs.meta similarity index 100% rename from Assets/Scripts/Player/PlayerMovement1.cs.meta rename to Assets/Scripts/Player/PlayerMovement.cs.meta diff --git a/Assets/Scripts/Player/PlayerMovement2.cs b/Assets/Scripts/Player/PlayerMovement2.cs deleted file mode 100644 index 30e2efe..0000000 --- a/Assets/Scripts/Player/PlayerMovement2.cs +++ /dev/null @@ -1,75 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; -using UnityEngine.InputSystem; - -public class PlayerMovement2 : MonoBehaviour -{ - public float moveSpeed = 5f; - private Rigidbody2D rb; - private Vector2 movement; - private Gamepad playerController; - - private bool right = false; - - private bool vibrate = false; - [SerializeField] private float stepCooldown = 0.05f; - [SerializeField] private float stepVibrationTime = 0.05f; - - [SerializeField] private GameObject rumble; - - private float cooldown; - private void Start() - { - rb = GetComponent(); - - playerController = Gamepad.all[1]; - - StartCoroutine(ToggleWithDelay()); - } - - void Update() - { - if (playerController != null) - { - movement.x = playerController.leftStick.x.ReadValue(); - movement.y = playerController.leftStick.y.ReadValue(); - } - else - { - movement.x = Input.GetAxisRaw("Horizontal"); - movement.y = Input.GetAxisRaw("Vertical"); ; - } - if (movement.x != 0 || movement.y != 0) - RumbleWalk(); - } - private void FixedUpdate() - { - rb.velocity = (movement * moveSpeed); - } - private void RumbleWalk() - { - if (vibrate && playerController != null) - { - if (right) - { - rumble.GetComponent().RumblePulse2(0.0f, 0.004f, stepVibrationTime); - right = false; - } - else if (!right) - { - rumble.GetComponent().RumblePulse2(0.004f, 0.0f, stepVibrationTime); - right = true; - } - vibrate = false; - } - } - private IEnumerator ToggleWithDelay() - { - while (true) - { - vibrate = !vibrate; - yield return new WaitForSeconds(stepCooldown); - } - } -} diff --git a/Assets/Scripts/Player/RopeWhipAttack.cs b/Assets/Scripts/Player/RopeWhipAttack.cs new file mode 100644 index 0000000..f8c8f09 --- /dev/null +++ b/Assets/Scripts/Player/RopeWhipAttack.cs @@ -0,0 +1,38 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.InputSystem; + +public class RopeWhipAttack : MonoBehaviour +{ + public RopeWhipAttack otherPlayerAttack; + public PlayerMovement playerMovement; + public RopeJoint joint; + + public bool IsWhippingOtherPlayer => joint.locked; + public bool IsBeingWhipped => otherPlayerAttack.joint.locked; + + public float initialDrag; + + private void Awake() + { + initialDrag = joint.body.drag; + } + + private void Update() + { + // Other player is whip attacking + if (otherPlayerAttack.joint.locked) return; + + if (Gamepad.all[playerMovement.player].rightShoulder.IsPressed()) + { + joint.locked = true; + otherPlayerAttack.joint.body.drag = 0f; + } + else + { + joint.locked = false; + otherPlayerAttack.joint.body.drag = otherPlayerAttack.initialDrag; + } + } +} diff --git a/Assets/Scripts/Player/PlayerMovement2.cs.meta b/Assets/Scripts/Player/RopeWhipAttack.cs.meta similarity index 83% rename from Assets/Scripts/Player/PlayerMovement2.cs.meta rename to Assets/Scripts/Player/RopeWhipAttack.cs.meta index 7853d45..36c86b7 100644 --- a/Assets/Scripts/Player/PlayerMovement2.cs.meta +++ b/Assets/Scripts/Player/RopeWhipAttack.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: e891644cda7544948af35b0d2f2c3be2 +guid: b69b92d72c7244443854899c4b700d9c MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/Scripts/Rope/RopeSimulator.cs b/Assets/Scripts/Rope/RopeSimulator.cs index dcd9bef..4da987b 100644 --- a/Assets/Scripts/Rope/RopeSimulator.cs +++ b/Assets/Scripts/Rope/RopeSimulator.cs @@ -142,12 +142,20 @@ public class RopeSimulator : MonoBehaviour Vector2 force = pullDirection * overshoot * (pullForce / divider); start.body.AddForce(force); } + else + { + start.body.velocity *= 0; + } if (!end.locked) { Vector2 pullDirection = (rope.points[rope.points.Count - 2].position - end.position).normalized; Vector2 force = pullDirection * overshoot * (pullForce / divider); end.body.AddForce(force); } + else + { + end.body.velocity *= 0; + } } // Handle squeze kills @@ -285,7 +293,6 @@ public class RopeSimulator : MonoBehaviour colliderToSquezeForce[hitCollider] = squezeForce + penetration.magnitude; p.position.Set(finalPos.x, finalPos.y, p.position.z); - } }