From 86a860126ec68abdaf16c87dc4b53206242015c0 Mon Sep 17 00:00:00 2001 From: hzmt Date: Sat, 31 Jan 2026 21:48:28 -0600 Subject: [PATCH 1/4] Fixing trick behavior --- Assets/Prefabs/Player.prefab | 4 +- Assets/Prefabs/Treat.prefab | 2 +- Assets/Prefabs/Trick.prefab | 8 +-- Assets/Scripts/TrickCollisionManager.cs | 19 +++++- Assets/Scripts/TrickController.cs | 80 ++++++++++--------------- 5 files changed, 58 insertions(+), 55 deletions(-) diff --git a/Assets/Prefabs/Player.prefab b/Assets/Prefabs/Player.prefab index 7bf77b5..b92b4ee 100644 --- a/Assets/Prefabs/Player.prefab +++ b/Assets/Prefabs/Player.prefab @@ -16,7 +16,7 @@ GameObject: - component: {fileID: 7763498473808654306} - component: {fileID: 3659140714710275197} - component: {fileID: 9133098163257611752} - m_Layer: 0 + m_Layer: 6 m_Name: Player m_TagString: Untagged m_Icon: {fileID: 0} @@ -251,7 +251,7 @@ GameObject: m_Component: - component: {fileID: 5569302833446856119} - component: {fileID: 6635970933810939283} - m_Layer: 0 + m_Layer: 6 m_Name: HoldLocation m_TagString: Untagged m_Icon: {fileID: 0} diff --git a/Assets/Prefabs/Treat.prefab b/Assets/Prefabs/Treat.prefab index 4577bf2..7bc8382 100644 --- a/Assets/Prefabs/Treat.prefab +++ b/Assets/Prefabs/Treat.prefab @@ -12,7 +12,7 @@ GameObject: - component: {fileID: 1016927753207964800} - component: {fileID: 2635922278447379203} - component: {fileID: 6828521680597590390} - m_Layer: 0 + m_Layer: 7 m_Name: Treat m_TagString: Interactable m_Icon: {fileID: 0} diff --git a/Assets/Prefabs/Trick.prefab b/Assets/Prefabs/Trick.prefab index ad14675..933e89f 100644 --- a/Assets/Prefabs/Trick.prefab +++ b/Assets/Prefabs/Trick.prefab @@ -186,11 +186,11 @@ MonoBehaviour: wanderInterval: 1.2 consumeCooldown: 5 trickPrefab: {fileID: 700000005} - avoidPlayerRadius: 1.2 - avoidTrickRadius: 0.8 + avoidPlayerRadius: 0 + avoidTrickRadius: 0 treatSeekWeight: 1 - avoidPlayerWeight: 0.25 - avoidTrickWeight: 0.2 + avoidPlayerWeight: 0 + avoidTrickWeight: 0 wanderWeight: 0.4 maxTricks: 50 jumpImpulse: 6 diff --git a/Assets/Scripts/TrickCollisionManager.cs b/Assets/Scripts/TrickCollisionManager.cs index 621ee01..de5f81e 100644 --- a/Assets/Scripts/TrickCollisionManager.cs +++ b/Assets/Scripts/TrickCollisionManager.cs @@ -2,6 +2,9 @@ using UnityEngine; public class TrickCollisionManager : MonoBehaviour { + [SerializeField] private string trickLayerName = "Trick"; + [SerializeField] private string playerLayerName = "Player"; + [SerializeField] private string treatLayerName = "Treat"; private bool initialized; private void Awake() @@ -16,7 +19,7 @@ public class TrickCollisionManager : MonoBehaviour return; } - var trickLayer = LayerMask.NameToLayer("Trick"); + var trickLayer = LayerMask.NameToLayer(trickLayerName); if (trickLayer == -1) { Debug.LogWarning("Trick layer not found. Create a layer named 'Trick' and assign it to Trick prefab."); @@ -24,6 +27,20 @@ public class TrickCollisionManager : MonoBehaviour } Physics2D.IgnoreLayerCollision(trickLayer, trickLayer, true); + IgnoreLayerPair(trickLayer, playerLayerName); + IgnoreLayerPair(trickLayer, treatLayerName); initialized = true; } + + private void IgnoreLayerPair(int trickLayer, string otherLayerName) + { + var otherLayer = LayerMask.NameToLayer(otherLayerName); + if (otherLayer == -1) + { + Debug.LogWarning($"Layer '{otherLayerName}' not found. Configure it if you want Trick to ignore collisions with it."); + return; + } + + Physics2D.IgnoreLayerCollision(trickLayer, otherLayer, true); + } } diff --git a/Assets/Scripts/TrickController.cs b/Assets/Scripts/TrickController.cs index f8a448f..a3db034 100644 --- a/Assets/Scripts/TrickController.cs +++ b/Assets/Scripts/TrickController.cs @@ -20,7 +20,6 @@ public class TrickController : MonoBehaviour [SerializeField] private float consumeCooldown = 1.5f; [SerializeField] private TrickController trickPrefab; [SerializeField] private float avoidPlayerRadius = 1.2f; - [SerializeField] private float avoidTrickRadius = 0.8f; [SerializeField] private float treatSeekWeight = 1f; [SerializeField] private float avoidPlayerWeight = 0.25f; [SerializeField] private float avoidTrickWeight = 0.2f; @@ -58,6 +57,8 @@ public class TrickController : MonoBehaviour { ActiveTricks.Add(this); } + + nextConsumeTime = Time.time + consumeCooldown; } private void OnDisable() @@ -118,29 +119,21 @@ public class TrickController : MonoBehaviour } Vector2 desiredVelocity; - var stopForTreat = false; - var avoidPlayerDirection = GetAvoidPlayerDirection(); - var avoidTrickDirection = GetAvoidTrickDirection(); var hasTarget = target != null; var canConsume = Time.time >= nextConsumeTime; var desiredX = 0f; - if (hasTarget) + if (hasTarget && canConsume) { var direction = ((Vector2)target.position - (Vector2)transform.position).normalized; desiredX += direction.x * treatSeekWeight; - stopForTreat = Vector2.Distance(transform.position, target.position) <= consumeDistance; } else { desiredX += wanderDirection.x * wanderWeight; } - if (!hasTarget || !canConsume) - { - desiredX += avoidPlayerDirection * avoidPlayerWeight; - } - desiredX += avoidTrickDirection * avoidTrickWeight; + // No trick-avoidance bias. if (Mathf.Abs(desiredX) < 0.01f) { @@ -150,12 +143,12 @@ public class TrickController : MonoBehaviour desiredVelocity = new Vector2(Mathf.Clamp(desiredX, -1f, 1f) * moveSpeed, 0f); var velocity = body.linearVelocity; - desiredXVelocity = stopForTreat ? 0f : desiredVelocity.x; + desiredXVelocity = desiredVelocity.x; velocity.x = Mathf.Lerp(velocity.x, desiredXVelocity, 0.25f); body.linearVelocity = velocity; var isMoving = Mathf.Abs(desiredXVelocity) > 0.1f; - TryJump(isMoving); + TryJump(isMoving, desiredXVelocity); } private Transform FindNearestTreat() @@ -219,6 +212,8 @@ public class TrickController : MonoBehaviour } var spawnPosition = hit.transform.position; + hit.enabled = false; + hit.gameObject.tag = "Untagged"; Destroy(hit.gameObject); if (target == hit.transform) { @@ -287,41 +282,8 @@ public class TrickController : MonoBehaviour return Mathf.Sign(origin.x - closest.position.x); } - private float GetAvoidTrickDirection() - { - if (ActiveTricks.Count <= 1) - { - return 0f; - } - Transform closest = null; - var closestDist = float.MaxValue; - var origin = transform.position; - for (var i = 0; i < ActiveTricks.Count; i++) - { - var candidate = ActiveTricks[i]; - if (candidate == null || candidate == this || !candidate.gameObject.activeInHierarchy) - { - continue; - } - - var dist = Vector2.Distance(origin, candidate.transform.position); - if (dist < closestDist) - { - closestDist = dist; - closest = candidate.transform; - } - } - - if (closest == null || closestDist > avoidTrickRadius) - { - return 0f; - } - - return Mathf.Sign(origin.x - closest.position.x); - } - - private void TryJump(bool isMoving) + private void TryJump(bool isMoving, float jumpDirection) { if (!isMoving || Time.time < nextJumpTime) { @@ -341,6 +303,12 @@ public class TrickController : MonoBehaviour var velocity = body.linearVelocity; velocity.y = 0f; + var jumpDir = Mathf.Sign(jumpDirection); + if (jumpDir == 0f) + { + jumpDir = Random.value < 0.5f ? -1f : 1f; + } + velocity.x = jumpDir * moveSpeed; body.linearVelocity = velocity; body.AddForce(Vector2.up * jumpImpulse, ForceMode2D.Impulse); nextJumpTime = Time.time + jumpCooldown; @@ -377,9 +345,27 @@ public class TrickController : MonoBehaviour if (carried) { target = null; + desiredXVelocity = 0f; var velocity = body.linearVelocity; velocity.x = 0f; + velocity.y = 0f; body.linearVelocity = velocity; + nextJumpTime = Time.time + 0.1f; + if (TryGetComponent(out var collider)) + { + collider.enabled = false; + } + } + else + { + var velocity = body.linearVelocity; + velocity.x = 0f; + velocity.y = 0f; + body.linearVelocity = velocity; + if (TryGetComponent(out var collider)) + { + collider.enabled = true; + } } } } From 36422bbbc6301e1e7ba79f3a928afde80cd186d9 Mon Sep 17 00:00:00 2001 From: hzmt Date: Sat, 31 Jan 2026 21:57:16 -0600 Subject: [PATCH 2/4] Adding spinning treat spawner --- Assets/Scenes/Experiment.unity | 53 ++++++++++++++++++++++++++++++++++ Assets/Scripts/Spawner.cs | 18 ++++++++++++ 2 files changed, 71 insertions(+) diff --git a/Assets/Scenes/Experiment.unity b/Assets/Scenes/Experiment.unity index 378ace2..0f2d73c 100644 --- a/Assets/Scenes/Experiment.unity +++ b/Assets/Scenes/Experiment.unity @@ -2642,6 +2642,58 @@ PrefabInstance: m_AddedGameObjects: [] m_AddedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 99cce68fc82caea44b5a140697be00a8, type: 3} +--- !u!1 &2200000000 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 2200000001} + - component: {fileID: 2200000002} + m_Layer: 0 + m_Name: Treat Spawner Center + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &2200000001 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2200000000} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &2200000002 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2200000000} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 14b0bf07aa1abc44a8ebd00cecacd6f4, type: 3} + m_Name: + m_EditorClassIdentifier: Assembly-CSharp::Spawner + objectToSpawn: {fileID: 262983251238298495, guid: 80c527a86ad6fc34dbc73ee80c1f2951, type: 3} + spawnInterval: 5 + maxAlive: 0 + spawnOnStart: 1 + repeatSpawns: 1 + launchForce: 4 + launchRandomAngle: 360 + spinSpeed: 180 --- !u!1660057539 &9223372036854775807 SceneRoots: m_ObjectHideFlags: 0 @@ -2671,3 +2723,4 @@ SceneRoots: - {fileID: 375061916} - {fileID: 2100000001} - {fileID: 2100000101} + - {fileID: 2200000001} diff --git a/Assets/Scripts/Spawner.cs b/Assets/Scripts/Spawner.cs index 051a10b..54805d9 100644 --- a/Assets/Scripts/Spawner.cs +++ b/Assets/Scripts/Spawner.cs @@ -11,6 +11,9 @@ public class Spawner : MonoBehaviour [SerializeField] private int maxAlive = 0; [SerializeField] private bool spawnOnStart = true; [SerializeField] private bool repeatSpawns = true; + [SerializeField] private float launchForce = 0f; + [SerializeField] private float launchRandomAngle = 0f; + [SerializeField] private float spinSpeed = 0f; private readonly List spawned = new List(); @@ -31,6 +34,14 @@ public class Spawner : MonoBehaviour } } + private void Update() + { + if (spinSpeed != 0f) + { + transform.Rotate(0f, 0f, spinSpeed * Time.deltaTime); + } + } + private IEnumerator SpawnRoutine() { // Infinite loop to keep spawning (can be modified) @@ -66,6 +77,13 @@ public class Spawner : MonoBehaviour var instance = Instantiate(objectToSpawn, transform.position, Quaternion.identity); spawned.Add(instance); + + if (launchForce > 0f && instance.TryGetComponent(out var body)) + { + var angleOffset = launchRandomAngle > 0f ? Random.Range(-launchRandomAngle, launchRandomAngle) : 0f; + var direction = Quaternion.Euler(0f, 0f, angleOffset) * transform.right; + body.AddForce(direction.normalized * launchForce, ForceMode2D.Impulse); + } } private int CountAlive() From e6593192a74cbcf324720d671e93da3e0630b89a Mon Sep 17 00:00:00 2001 From: hzmt Date: Sat, 31 Jan 2026 22:03:18 -0600 Subject: [PATCH 3/4] Adding trick respawn logic --- Assets/Prefabs/Trick.prefab | 5 ++++- Assets/Scenes/Experiment.unity | 18 +++++++++++++++-- Assets/Scripts/Spawner.cs | 5 +++++ Assets/Scripts/TrickController.cs | 11 ++++++++++ Assets/Scripts/TrickSpawnKeeper.cs | 27 +++++++++++++++++++++++++ Assets/Scripts/TrickSpawnKeeper.cs.meta | 11 ++++++++++ 6 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 Assets/Scripts/TrickSpawnKeeper.cs create mode 100644 Assets/Scripts/TrickSpawnKeeper.cs.meta diff --git a/Assets/Prefabs/Trick.prefab b/Assets/Prefabs/Trick.prefab index 933e89f..abf6bb5 100644 --- a/Assets/Prefabs/Trick.prefab +++ b/Assets/Prefabs/Trick.prefab @@ -184,7 +184,7 @@ MonoBehaviour: targetRefreshInterval: 0.4 consumeDistance: 0.35 wanderInterval: 1.2 - consumeCooldown: 5 + consumeCooldown: 3 trickPrefab: {fileID: 700000005} avoidPlayerRadius: 0 avoidTrickRadius: 0 @@ -200,3 +200,6 @@ MonoBehaviour: serializedVersion: 2 m_Bits: 4294967295 groundCheckDistance: 0.05 + despawnMinX: -12 + despawnMaxX: 12 + despawnMinY: -10 diff --git a/Assets/Scenes/Experiment.unity b/Assets/Scenes/Experiment.unity index 0f2d73c..ef30e47 100644 --- a/Assets/Scenes/Experiment.unity +++ b/Assets/Scenes/Experiment.unity @@ -2521,10 +2521,10 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: Assembly-CSharp::Spawner objectToSpawn: {fileID: 700000000, guid: 8bf8957665594a718535d06aba0cdee7, type: 3} - spawnInterval: 3 + spawnInterval: 5 maxAlive: 1 spawnOnStart: 1 - repeatSpawns: 0 + repeatSpawns: 1 --- !u!1 &2100000100 GameObject: m_ObjectHideFlags: 0 @@ -2569,6 +2569,20 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 44f2fea877e8498fa4898917356fb2e6, type: 3} m_Name: m_EditorClassIdentifier: +--- !u!114 &2100000103 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2100000100} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 4eb1753179e848ed8662b6f039a33df9, type: 3} + m_Name: + m_EditorClassIdentifier: + spawner: {fileID: 2100000002} + checkInterval: 1 --- !u!1001 &1760117322204329348 PrefabInstance: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Spawner.cs b/Assets/Scripts/Spawner.cs index 54805d9..4fc23fe 100644 --- a/Assets/Scripts/Spawner.cs +++ b/Assets/Scripts/Spawner.cs @@ -42,6 +42,11 @@ public class Spawner : MonoBehaviour } } + public void SpawnNow() + { + SpawnOnce(); + } + private IEnumerator SpawnRoutine() { // Infinite loop to keep spawning (can be modified) diff --git a/Assets/Scripts/TrickController.cs b/Assets/Scripts/TrickController.cs index a3db034..080a470 100644 --- a/Assets/Scripts/TrickController.cs +++ b/Assets/Scripts/TrickController.cs @@ -11,6 +11,7 @@ public class TrickController : MonoBehaviour private static readonly List ActiveTricks = new List(); private static PlayerPlatformerController[] cachedPlayers = Array.Empty(); private static float nextPlayerCacheTime; + public static int ActiveCount => ActiveTricks.Count; [SerializeField] private string treatTag = "Interactable"; [SerializeField] private float moveSpeed = 3.5f; @@ -30,6 +31,9 @@ public class TrickController : MonoBehaviour [SerializeField] private float jumpChancePerSecond = 0.5f; [SerializeField] private LayerMask groundMask = ~0; [SerializeField] private float groundCheckDistance = 0.05f; + [SerializeField] private float despawnMinX = -12f; + [SerializeField] private float despawnMaxX = 12f; + [SerializeField] private float despawnMinY = -10f; private Rigidbody2D body; private Transform target; @@ -74,6 +78,13 @@ public class TrickController : MonoBehaviour return; } + var position = transform.position; + if (position.x < despawnMinX || position.x > despawnMaxX || position.y < despawnMinY) + { + Destroy(gameObject); + return; + } + if (Time.time >= nextTargetTime) { target = FindNearestTreat(); diff --git a/Assets/Scripts/TrickSpawnKeeper.cs b/Assets/Scripts/TrickSpawnKeeper.cs new file mode 100644 index 0000000..cbeb6c0 --- /dev/null +++ b/Assets/Scripts/TrickSpawnKeeper.cs @@ -0,0 +1,27 @@ +using UnityEngine; + +public class TrickSpawnKeeper : MonoBehaviour +{ + [SerializeField] private Spawner spawner; + [SerializeField] private float checkInterval = 1f; + private float nextCheckTime; + + private void Update() + { + if (Time.time < nextCheckTime) + { + return; + } + + nextCheckTime = Time.time + checkInterval; + if (spawner == null) + { + return; + } + + if (TrickController.ActiveCount == 0) + { + spawner.SpawnNow(); + } + } +} diff --git a/Assets/Scripts/TrickSpawnKeeper.cs.meta b/Assets/Scripts/TrickSpawnKeeper.cs.meta new file mode 100644 index 0000000..6fdd91f --- /dev/null +++ b/Assets/Scripts/TrickSpawnKeeper.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4eb1753179e848ed8662b6f039a33df9 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From c47bd0fe279659f9bfcda33a7aae2b2224fa4a12 Mon Sep 17 00:00:00 2001 From: hzmt Date: Sat, 31 Jan 2026 22:27:43 -0600 Subject: [PATCH 4/4] yeet --- Assets/Prefabs/Trick.prefab | 2 - Assets/Scripts/PlayerPickupController.cs | 92 +++++++++++++++++++- Assets/Scripts/PlayerPlatformerController.cs | 5 ++ Assets/Scripts/TrickController.cs | 14 ++- 4 files changed, 107 insertions(+), 6 deletions(-) diff --git a/Assets/Prefabs/Trick.prefab b/Assets/Prefabs/Trick.prefab index abf6bb5..abc688a 100644 --- a/Assets/Prefabs/Trick.prefab +++ b/Assets/Prefabs/Trick.prefab @@ -189,8 +189,6 @@ MonoBehaviour: avoidPlayerRadius: 0 avoidTrickRadius: 0 treatSeekWeight: 1 - avoidPlayerWeight: 0 - avoidTrickWeight: 0 wanderWeight: 0.4 maxTricks: 50 jumpImpulse: 6 diff --git a/Assets/Scripts/PlayerPickupController.cs b/Assets/Scripts/PlayerPickupController.cs index 6f50eb8..6a869ed 100644 --- a/Assets/Scripts/PlayerPickupController.cs +++ b/Assets/Scripts/PlayerPickupController.cs @@ -12,15 +12,24 @@ public class PlayerPickupController : MonoBehaviour [SerializeField] private string controlSchemeName = ""; [SerializeField] private int gamepadIndex = -1; private bool deviceFilterEnabled; + [SerializeField] private float minThrowSpeed = 6f; + [SerializeField] private float maxThrowSpeed = 20f; + [SerializeField] private float throwChargeTime = 0.75f; + [SerializeField] private float throwUpwardForce = 0f; + [SerializeField] private float throwTriggerThreshold = 0.6f; [Header("Setup")] public Transform holdPoint; // Empty GameObject above head public float pickupRange = 2.0f; private GameObject heldObject; + private PlayerPlatformerController platformerController; + private bool isChargingThrow; + private float throwChargeStartTime; private void OnEnable() { + platformerController = GetComponent(); if (inputActions == null) { Debug.LogError("PlayerPickupController missing InputActionAsset reference.", this); @@ -61,6 +70,7 @@ public class PlayerPickupController : MonoBehaviour } InputSystem.onDeviceChange -= HandleDeviceChange; + platformerController = null; } private void OnInteractPerformed(InputAction.CallbackContext context) @@ -69,13 +79,46 @@ public class PlayerPickupController : MonoBehaviour if(heldObject != null) { Debug.Log("Dropping held object"); - DropObject(); + DropObject(false); return; } TryPickUp(); } + private void Update() + { + if (heldObject == null) + { + return; + } + + if (!heldObject.TryGetComponent(out _)) + { + return; + } + + var gamepad = GetGamepad(); + if (gamepad == null) + { + return; + } + + var triggerValue = gamepad.rightTrigger.ReadValue(); + if (triggerValue >= throwTriggerThreshold) + { + if (!isChargingThrow) + { + isChargingThrow = true; + throwChargeStartTime = Time.time; + } + } + else if (isChargingThrow) + { + DropObject(true); + } + } + void TryPickUp() { // Find all colliders within range @@ -129,17 +172,22 @@ public class PlayerPickupController : MonoBehaviour } } - void DropObject() + void DropObject(bool applyThrow) { // Re-enable physics if (heldObject.TryGetComponent(out Rigidbody2D rb)) { rb.bodyType = RigidbodyType2D.Dynamic; rb.gravityScale = 1; + rb.linearVelocity = Vector2.zero; } if (heldObject.TryGetComponent(out var trick)) { trick.SetCarried(false); + if (applyThrow) + { + trick.OnThrown(); + } } if (heldObject.TryGetComponent(out var playerController)) { @@ -149,12 +197,52 @@ public class PlayerPickupController : MonoBehaviour heldPickup.enabled = true; } } + if (applyThrow && heldObject.TryGetComponent(out Rigidbody2D throwBody)) + { + var direction = 1f; + if (platformerController != null && Mathf.Abs(platformerController.LastMoveX) > 0.01f) + { + direction = Mathf.Sign(platformerController.LastMoveX); + } + var charge01 = 1f; + if (isChargingThrow) + { + var heldTime = Time.time - throwChargeStartTime; + charge01 = Mathf.Clamp01(heldTime / Mathf.Max(0.01f, throwChargeTime)); + } + var throwSpeed = Mathf.Lerp(minThrowSpeed, maxThrowSpeed, charge01); + throwBody.linearVelocity = new Vector2(direction * throwSpeed, 0f); + throwBody.angularVelocity = 0f; + } Collider2D col = heldObject.GetComponent(); if (col != null) col.enabled = true; // Unparent heldObject.transform.SetParent(null); heldObject = null; + isChargingThrow = false; + } + + private Gamepad GetGamepad() + { + if (runtimeActions == null) + { + return null; + } + + var devices = runtimeActions.devices; + if (devices != null) + { + foreach (var device in devices) + { + if (device is Gamepad gamepad) + { + return gamepad; + } + } + } + + return Gamepad.current; } private bool ShouldFilterDevices() diff --git a/Assets/Scripts/PlayerPlatformerController.cs b/Assets/Scripts/PlayerPlatformerController.cs index 2562486..97d4962 100644 --- a/Assets/Scripts/PlayerPlatformerController.cs +++ b/Assets/Scripts/PlayerPlatformerController.cs @@ -22,6 +22,7 @@ public class PlayerPlatformerController : MonoBehaviour private InputActionAsset runtimeActions; private InputAction moveAction; private InputAction jumpAction; + public float LastMoveX { get; private set; } private bool deviceFilterEnabled; private void Awake() @@ -86,6 +87,10 @@ public class PlayerPlatformerController : MonoBehaviour } var input = moveAction.ReadValue(); + if (Mathf.Abs(input.x) > 0.01f) + { + LastMoveX = input.x; + } var velocity = body.linearVelocity; velocity.x = input.x * moveSpeed; body.linearVelocity = velocity; diff --git a/Assets/Scripts/TrickController.cs b/Assets/Scripts/TrickController.cs index 080a470..f1775d2 100644 --- a/Assets/Scripts/TrickController.cs +++ b/Assets/Scripts/TrickController.cs @@ -22,8 +22,6 @@ public class TrickController : MonoBehaviour [SerializeField] private TrickController trickPrefab; [SerializeField] private float avoidPlayerRadius = 1.2f; [SerializeField] private float treatSeekWeight = 1f; - [SerializeField] private float avoidPlayerWeight = 0.25f; - [SerializeField] private float avoidTrickWeight = 0.2f; [SerializeField] private float wanderWeight = 0.4f; [SerializeField] private int maxTricks = 50; [SerializeField] private float jumpImpulse = 6f; @@ -34,6 +32,7 @@ public class TrickController : MonoBehaviour [SerializeField] private float despawnMinX = -12f; [SerializeField] private float despawnMaxX = 12f; [SerializeField] private float despawnMinY = -10f; + [SerializeField] private float throwControlLockTime = 0.35f; private Rigidbody2D body; private Transform target; @@ -44,6 +43,7 @@ public class TrickController : MonoBehaviour private float nextConsumeTime; private float desiredXVelocity; private float nextJumpTime; + private float movementLockUntil; private void Awake() { @@ -121,6 +121,11 @@ public class TrickController : MonoBehaviour return; } + if (Time.time < movementLockUntil) + { + return; + } + if (!IsGrounded()) { var airborne = body.linearVelocity; @@ -379,4 +384,9 @@ public class TrickController : MonoBehaviour } } } + + public void OnThrown() + { + movementLockUntil = Time.time + throwControlLockTime; + } }