This commit is contained in:
hz
2026-01-31 22:27:43 -06:00
parent e6593192a7
commit c47bd0fe27
4 changed files with 107 additions and 6 deletions
-2
View File
@@ -189,8 +189,6 @@ MonoBehaviour:
avoidPlayerRadius: 0 avoidPlayerRadius: 0
avoidTrickRadius: 0 avoidTrickRadius: 0
treatSeekWeight: 1 treatSeekWeight: 1
avoidPlayerWeight: 0
avoidTrickWeight: 0
wanderWeight: 0.4 wanderWeight: 0.4
maxTricks: 50 maxTricks: 50
jumpImpulse: 6 jumpImpulse: 6
+90 -2
View File
@@ -12,15 +12,24 @@ public class PlayerPickupController : MonoBehaviour
[SerializeField] private string controlSchemeName = ""; [SerializeField] private string controlSchemeName = "";
[SerializeField] private int gamepadIndex = -1; [SerializeField] private int gamepadIndex = -1;
private bool deviceFilterEnabled; 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")] [Header("Setup")]
public Transform holdPoint; // Empty GameObject above head public Transform holdPoint; // Empty GameObject above head
public float pickupRange = 2.0f; public float pickupRange = 2.0f;
private GameObject heldObject; private GameObject heldObject;
private PlayerPlatformerController platformerController;
private bool isChargingThrow;
private float throwChargeStartTime;
private void OnEnable() private void OnEnable()
{ {
platformerController = GetComponent<PlayerPlatformerController>();
if (inputActions == null) if (inputActions == null)
{ {
Debug.LogError("PlayerPickupController missing InputActionAsset reference.", this); Debug.LogError("PlayerPickupController missing InputActionAsset reference.", this);
@@ -61,6 +70,7 @@ public class PlayerPickupController : MonoBehaviour
} }
InputSystem.onDeviceChange -= HandleDeviceChange; InputSystem.onDeviceChange -= HandleDeviceChange;
platformerController = null;
} }
private void OnInteractPerformed(InputAction.CallbackContext context) private void OnInteractPerformed(InputAction.CallbackContext context)
@@ -69,13 +79,46 @@ public class PlayerPickupController : MonoBehaviour
if(heldObject != null) if(heldObject != null)
{ {
Debug.Log("Dropping held object"); Debug.Log("Dropping held object");
DropObject(); DropObject(false);
return; return;
} }
TryPickUp(); TryPickUp();
} }
private void Update()
{
if (heldObject == null)
{
return;
}
if (!heldObject.TryGetComponent<TrickController>(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() void TryPickUp()
{ {
// Find all colliders within range // Find all colliders within range
@@ -129,17 +172,22 @@ public class PlayerPickupController : MonoBehaviour
} }
} }
void DropObject() void DropObject(bool applyThrow)
{ {
// Re-enable physics // Re-enable physics
if (heldObject.TryGetComponent<Rigidbody2D>(out Rigidbody2D rb)) if (heldObject.TryGetComponent<Rigidbody2D>(out Rigidbody2D rb))
{ {
rb.bodyType = RigidbodyType2D.Dynamic; rb.bodyType = RigidbodyType2D.Dynamic;
rb.gravityScale = 1; rb.gravityScale = 1;
rb.linearVelocity = Vector2.zero;
} }
if (heldObject.TryGetComponent<TrickController>(out var trick)) if (heldObject.TryGetComponent<TrickController>(out var trick))
{ {
trick.SetCarried(false); trick.SetCarried(false);
if (applyThrow)
{
trick.OnThrown();
}
} }
if (heldObject.TryGetComponent<PlayerPlatformerController>(out var playerController)) if (heldObject.TryGetComponent<PlayerPlatformerController>(out var playerController))
{ {
@@ -149,12 +197,52 @@ public class PlayerPickupController : MonoBehaviour
heldPickup.enabled = true; heldPickup.enabled = true;
} }
} }
if (applyThrow && heldObject.TryGetComponent<Rigidbody2D>(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<Collider2D>(); Collider2D col = heldObject.GetComponent<Collider2D>();
if (col != null) col.enabled = true; if (col != null) col.enabled = true;
// Unparent // Unparent
heldObject.transform.SetParent(null); heldObject.transform.SetParent(null);
heldObject = 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() private bool ShouldFilterDevices()
@@ -22,6 +22,7 @@ public class PlayerPlatformerController : MonoBehaviour
private InputActionAsset runtimeActions; private InputActionAsset runtimeActions;
private InputAction moveAction; private InputAction moveAction;
private InputAction jumpAction; private InputAction jumpAction;
public float LastMoveX { get; private set; }
private bool deviceFilterEnabled; private bool deviceFilterEnabled;
private void Awake() private void Awake()
@@ -86,6 +87,10 @@ public class PlayerPlatformerController : MonoBehaviour
} }
var input = moveAction.ReadValue<Vector2>(); var input = moveAction.ReadValue<Vector2>();
if (Mathf.Abs(input.x) > 0.01f)
{
LastMoveX = input.x;
}
var velocity = body.linearVelocity; var velocity = body.linearVelocity;
velocity.x = input.x * moveSpeed; velocity.x = input.x * moveSpeed;
body.linearVelocity = velocity; body.linearVelocity = velocity;
+12 -2
View File
@@ -22,8 +22,6 @@ public class TrickController : MonoBehaviour
[SerializeField] private TrickController trickPrefab; [SerializeField] private TrickController trickPrefab;
[SerializeField] private float avoidPlayerRadius = 1.2f; [SerializeField] private float avoidPlayerRadius = 1.2f;
[SerializeField] private float treatSeekWeight = 1f; [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 float wanderWeight = 0.4f;
[SerializeField] private int maxTricks = 50; [SerializeField] private int maxTricks = 50;
[SerializeField] private float jumpImpulse = 6f; [SerializeField] private float jumpImpulse = 6f;
@@ -34,6 +32,7 @@ public class TrickController : MonoBehaviour
[SerializeField] private float despawnMinX = -12f; [SerializeField] private float despawnMinX = -12f;
[SerializeField] private float despawnMaxX = 12f; [SerializeField] private float despawnMaxX = 12f;
[SerializeField] private float despawnMinY = -10f; [SerializeField] private float despawnMinY = -10f;
[SerializeField] private float throwControlLockTime = 0.35f;
private Rigidbody2D body; private Rigidbody2D body;
private Transform target; private Transform target;
@@ -44,6 +43,7 @@ public class TrickController : MonoBehaviour
private float nextConsumeTime; private float nextConsumeTime;
private float desiredXVelocity; private float desiredXVelocity;
private float nextJumpTime; private float nextJumpTime;
private float movementLockUntil;
private void Awake() private void Awake()
{ {
@@ -121,6 +121,11 @@ public class TrickController : MonoBehaviour
return; return;
} }
if (Time.time < movementLockUntil)
{
return;
}
if (!IsGrounded()) if (!IsGrounded())
{ {
var airborne = body.linearVelocity; var airborne = body.linearVelocity;
@@ -379,4 +384,9 @@ public class TrickController : MonoBehaviour
} }
} }
} }
public void OnThrown()
{
movementLockUntil = Time.time + throwControlLockTime;
}
} }