Merge pull request 'Fixing trick behavior' (#36) from get-tricky2 into main
Reviewed-on: admin/GGJ2026#36 Reviewed-by: Ramdar <[email protected]>
This commit is contained in:
@@ -16,7 +16,7 @@ GameObject:
|
|||||||
- component: {fileID: 7763498473808654306}
|
- component: {fileID: 7763498473808654306}
|
||||||
- component: {fileID: 3659140714710275197}
|
- component: {fileID: 3659140714710275197}
|
||||||
- component: {fileID: 9133098163257611752}
|
- component: {fileID: 9133098163257611752}
|
||||||
m_Layer: 0
|
m_Layer: 6
|
||||||
m_Name: Player
|
m_Name: Player
|
||||||
m_TagString: Untagged
|
m_TagString: Untagged
|
||||||
m_Icon: {fileID: 0}
|
m_Icon: {fileID: 0}
|
||||||
@@ -251,7 +251,7 @@ GameObject:
|
|||||||
m_Component:
|
m_Component:
|
||||||
- component: {fileID: 5569302833446856119}
|
- component: {fileID: 5569302833446856119}
|
||||||
- component: {fileID: 6635970933810939283}
|
- component: {fileID: 6635970933810939283}
|
||||||
m_Layer: 0
|
m_Layer: 6
|
||||||
m_Name: HoldLocation
|
m_Name: HoldLocation
|
||||||
m_TagString: Untagged
|
m_TagString: Untagged
|
||||||
m_Icon: {fileID: 0}
|
m_Icon: {fileID: 0}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ GameObject:
|
|||||||
- component: {fileID: 1016927753207964800}
|
- component: {fileID: 1016927753207964800}
|
||||||
- component: {fileID: 2635922278447379203}
|
- component: {fileID: 2635922278447379203}
|
||||||
- component: {fileID: 6828521680597590390}
|
- component: {fileID: 6828521680597590390}
|
||||||
m_Layer: 0
|
m_Layer: 7
|
||||||
m_Name: Treat
|
m_Name: Treat
|
||||||
m_TagString: Interactable
|
m_TagString: Interactable
|
||||||
m_Icon: {fileID: 0}
|
m_Icon: {fileID: 0}
|
||||||
|
|||||||
@@ -184,13 +184,11 @@ MonoBehaviour:
|
|||||||
targetRefreshInterval: 0.4
|
targetRefreshInterval: 0.4
|
||||||
consumeDistance: 0.35
|
consumeDistance: 0.35
|
||||||
wanderInterval: 1.2
|
wanderInterval: 1.2
|
||||||
consumeCooldown: 5
|
consumeCooldown: 3
|
||||||
trickPrefab: {fileID: 700000005}
|
trickPrefab: {fileID: 700000005}
|
||||||
avoidPlayerRadius: 1.2
|
avoidPlayerRadius: 0
|
||||||
avoidTrickRadius: 0.8
|
avoidTrickRadius: 0
|
||||||
treatSeekWeight: 1
|
treatSeekWeight: 1
|
||||||
avoidPlayerWeight: 0.25
|
|
||||||
avoidTrickWeight: 0.2
|
|
||||||
wanderWeight: 0.4
|
wanderWeight: 0.4
|
||||||
maxTricks: 50
|
maxTricks: 50
|
||||||
jumpImpulse: 6
|
jumpImpulse: 6
|
||||||
@@ -200,3 +198,6 @@ MonoBehaviour:
|
|||||||
serializedVersion: 2
|
serializedVersion: 2
|
||||||
m_Bits: 4294967295
|
m_Bits: 4294967295
|
||||||
groundCheckDistance: 0.05
|
groundCheckDistance: 0.05
|
||||||
|
despawnMinX: -12
|
||||||
|
despawnMaxX: 12
|
||||||
|
despawnMinY: -10
|
||||||
|
|||||||
@@ -2521,10 +2521,10 @@ MonoBehaviour:
|
|||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier: Assembly-CSharp::Spawner
|
m_EditorClassIdentifier: Assembly-CSharp::Spawner
|
||||||
objectToSpawn: {fileID: 700000000, guid: 8bf8957665594a718535d06aba0cdee7, type: 3}
|
objectToSpawn: {fileID: 700000000, guid: 8bf8957665594a718535d06aba0cdee7, type: 3}
|
||||||
spawnInterval: 3
|
spawnInterval: 5
|
||||||
maxAlive: 1
|
maxAlive: 1
|
||||||
spawnOnStart: 1
|
spawnOnStart: 1
|
||||||
repeatSpawns: 0
|
repeatSpawns: 1
|
||||||
--- !u!1 &2100000100
|
--- !u!1 &2100000100
|
||||||
GameObject:
|
GameObject:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
@@ -2569,6 +2569,20 @@ MonoBehaviour:
|
|||||||
m_Script: {fileID: 11500000, guid: 44f2fea877e8498fa4898917356fb2e6, type: 3}
|
m_Script: {fileID: 11500000, guid: 44f2fea877e8498fa4898917356fb2e6, type: 3}
|
||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier:
|
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
|
--- !u!1001 &1760117322204329348
|
||||||
PrefabInstance:
|
PrefabInstance:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
@@ -2642,6 +2656,58 @@ PrefabInstance:
|
|||||||
m_AddedGameObjects: []
|
m_AddedGameObjects: []
|
||||||
m_AddedComponents: []
|
m_AddedComponents: []
|
||||||
m_SourcePrefab: {fileID: 100100000, guid: 99cce68fc82caea44b5a140697be00a8, type: 3}
|
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
|
--- !u!1660057539 &9223372036854775807
|
||||||
SceneRoots:
|
SceneRoots:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
@@ -2671,3 +2737,4 @@ SceneRoots:
|
|||||||
- {fileID: 375061916}
|
- {fileID: 375061916}
|
||||||
- {fileID: 2100000001}
|
- {fileID: 2100000001}
|
||||||
- {fileID: 2100000101}
|
- {fileID: 2100000101}
|
||||||
|
- {fileID: 2200000001}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -11,6 +11,9 @@ public class Spawner : MonoBehaviour
|
|||||||
[SerializeField] private int maxAlive = 0;
|
[SerializeField] private int maxAlive = 0;
|
||||||
[SerializeField] private bool spawnOnStart = true;
|
[SerializeField] private bool spawnOnStart = true;
|
||||||
[SerializeField] private bool repeatSpawns = true;
|
[SerializeField] private bool repeatSpawns = true;
|
||||||
|
[SerializeField] private float launchForce = 0f;
|
||||||
|
[SerializeField] private float launchRandomAngle = 0f;
|
||||||
|
[SerializeField] private float spinSpeed = 0f;
|
||||||
|
|
||||||
private readonly List<GameObject> spawned = new List<GameObject>();
|
private readonly List<GameObject> spawned = new List<GameObject>();
|
||||||
|
|
||||||
@@ -31,6 +34,19 @@ public class Spawner : MonoBehaviour
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void Update()
|
||||||
|
{
|
||||||
|
if (spinSpeed != 0f)
|
||||||
|
{
|
||||||
|
transform.Rotate(0f, 0f, spinSpeed * Time.deltaTime);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SpawnNow()
|
||||||
|
{
|
||||||
|
SpawnOnce();
|
||||||
|
}
|
||||||
|
|
||||||
private IEnumerator SpawnRoutine()
|
private IEnumerator SpawnRoutine()
|
||||||
{
|
{
|
||||||
// Infinite loop to keep spawning (can be modified)
|
// Infinite loop to keep spawning (can be modified)
|
||||||
@@ -66,6 +82,13 @@ public class Spawner : MonoBehaviour
|
|||||||
|
|
||||||
var instance = Instantiate(objectToSpawn, transform.position, Quaternion.identity);
|
var instance = Instantiate(objectToSpawn, transform.position, Quaternion.identity);
|
||||||
spawned.Add(instance);
|
spawned.Add(instance);
|
||||||
|
|
||||||
|
if (launchForce > 0f && instance.TryGetComponent<Rigidbody2D>(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()
|
private int CountAlive()
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ using UnityEngine;
|
|||||||
|
|
||||||
public class TrickCollisionManager : MonoBehaviour
|
public class TrickCollisionManager : MonoBehaviour
|
||||||
{
|
{
|
||||||
|
[SerializeField] private string trickLayerName = "Trick";
|
||||||
|
[SerializeField] private string playerLayerName = "Player";
|
||||||
|
[SerializeField] private string treatLayerName = "Treat";
|
||||||
private bool initialized;
|
private bool initialized;
|
||||||
|
|
||||||
private void Awake()
|
private void Awake()
|
||||||
@@ -16,7 +19,7 @@ public class TrickCollisionManager : MonoBehaviour
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var trickLayer = LayerMask.NameToLayer("Trick");
|
var trickLayer = LayerMask.NameToLayer(trickLayerName);
|
||||||
if (trickLayer == -1)
|
if (trickLayer == -1)
|
||||||
{
|
{
|
||||||
Debug.LogWarning("Trick layer not found. Create a layer named 'Trick' and assign it to Trick prefab.");
|
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);
|
Physics2D.IgnoreLayerCollision(trickLayer, trickLayer, true);
|
||||||
|
IgnoreLayerPair(trickLayer, playerLayerName);
|
||||||
|
IgnoreLayerPair(trickLayer, treatLayerName);
|
||||||
initialized = true;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ public class TrickController : MonoBehaviour
|
|||||||
private static readonly List<TrickController> ActiveTricks = new List<TrickController>();
|
private static readonly List<TrickController> ActiveTricks = new List<TrickController>();
|
||||||
private static PlayerPlatformerController[] cachedPlayers = Array.Empty<PlayerPlatformerController>();
|
private static PlayerPlatformerController[] cachedPlayers = Array.Empty<PlayerPlatformerController>();
|
||||||
private static float nextPlayerCacheTime;
|
private static float nextPlayerCacheTime;
|
||||||
|
public static int ActiveCount => ActiveTricks.Count;
|
||||||
|
|
||||||
[SerializeField] private string treatTag = "Interactable";
|
[SerializeField] private string treatTag = "Interactable";
|
||||||
[SerializeField] private float moveSpeed = 3.5f;
|
[SerializeField] private float moveSpeed = 3.5f;
|
||||||
@@ -20,10 +21,7 @@ public class TrickController : MonoBehaviour
|
|||||||
[SerializeField] private float consumeCooldown = 1.5f;
|
[SerializeField] private float consumeCooldown = 1.5f;
|
||||||
[SerializeField] private TrickController trickPrefab;
|
[SerializeField] private TrickController trickPrefab;
|
||||||
[SerializeField] private float avoidPlayerRadius = 1.2f;
|
[SerializeField] private float avoidPlayerRadius = 1.2f;
|
||||||
[SerializeField] private float avoidTrickRadius = 0.8f;
|
|
||||||
[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;
|
||||||
@@ -31,6 +29,10 @@ public class TrickController : MonoBehaviour
|
|||||||
[SerializeField] private float jumpChancePerSecond = 0.5f;
|
[SerializeField] private float jumpChancePerSecond = 0.5f;
|
||||||
[SerializeField] private LayerMask groundMask = ~0;
|
[SerializeField] private LayerMask groundMask = ~0;
|
||||||
[SerializeField] private float groundCheckDistance = 0.05f;
|
[SerializeField] private float groundCheckDistance = 0.05f;
|
||||||
|
[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 Rigidbody2D body;
|
||||||
private Transform target;
|
private Transform target;
|
||||||
@@ -41,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()
|
||||||
{
|
{
|
||||||
@@ -58,6 +61,8 @@ public class TrickController : MonoBehaviour
|
|||||||
{
|
{
|
||||||
ActiveTricks.Add(this);
|
ActiveTricks.Add(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
nextConsumeTime = Time.time + consumeCooldown;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnDisable()
|
private void OnDisable()
|
||||||
@@ -73,6 +78,13 @@ public class TrickController : MonoBehaviour
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var position = transform.position;
|
||||||
|
if (position.x < despawnMinX || position.x > despawnMaxX || position.y < despawnMinY)
|
||||||
|
{
|
||||||
|
Destroy(gameObject);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (Time.time >= nextTargetTime)
|
if (Time.time >= nextTargetTime)
|
||||||
{
|
{
|
||||||
target = FindNearestTreat();
|
target = FindNearestTreat();
|
||||||
@@ -109,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;
|
||||||
@@ -118,29 +135,21 @@ public class TrickController : MonoBehaviour
|
|||||||
}
|
}
|
||||||
|
|
||||||
Vector2 desiredVelocity;
|
Vector2 desiredVelocity;
|
||||||
var stopForTreat = false;
|
|
||||||
var avoidPlayerDirection = GetAvoidPlayerDirection();
|
|
||||||
var avoidTrickDirection = GetAvoidTrickDirection();
|
|
||||||
var hasTarget = target != null;
|
var hasTarget = target != null;
|
||||||
var canConsume = Time.time >= nextConsumeTime;
|
var canConsume = Time.time >= nextConsumeTime;
|
||||||
|
|
||||||
var desiredX = 0f;
|
var desiredX = 0f;
|
||||||
if (hasTarget)
|
if (hasTarget && canConsume)
|
||||||
{
|
{
|
||||||
var direction = ((Vector2)target.position - (Vector2)transform.position).normalized;
|
var direction = ((Vector2)target.position - (Vector2)transform.position).normalized;
|
||||||
desiredX += direction.x * treatSeekWeight;
|
desiredX += direction.x * treatSeekWeight;
|
||||||
stopForTreat = Vector2.Distance(transform.position, target.position) <= consumeDistance;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
desiredX += wanderDirection.x * wanderWeight;
|
desiredX += wanderDirection.x * wanderWeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!hasTarget || !canConsume)
|
// No trick-avoidance bias.
|
||||||
{
|
|
||||||
desiredX += avoidPlayerDirection * avoidPlayerWeight;
|
|
||||||
}
|
|
||||||
desiredX += avoidTrickDirection * avoidTrickWeight;
|
|
||||||
|
|
||||||
if (Mathf.Abs(desiredX) < 0.01f)
|
if (Mathf.Abs(desiredX) < 0.01f)
|
||||||
{
|
{
|
||||||
@@ -150,12 +159,12 @@ public class TrickController : MonoBehaviour
|
|||||||
desiredVelocity = new Vector2(Mathf.Clamp(desiredX, -1f, 1f) * moveSpeed, 0f);
|
desiredVelocity = new Vector2(Mathf.Clamp(desiredX, -1f, 1f) * moveSpeed, 0f);
|
||||||
|
|
||||||
var velocity = body.linearVelocity;
|
var velocity = body.linearVelocity;
|
||||||
desiredXVelocity = stopForTreat ? 0f : desiredVelocity.x;
|
desiredXVelocity = desiredVelocity.x;
|
||||||
velocity.x = Mathf.Lerp(velocity.x, desiredXVelocity, 0.25f);
|
velocity.x = Mathf.Lerp(velocity.x, desiredXVelocity, 0.25f);
|
||||||
body.linearVelocity = velocity;
|
body.linearVelocity = velocity;
|
||||||
|
|
||||||
var isMoving = Mathf.Abs(desiredXVelocity) > 0.1f;
|
var isMoving = Mathf.Abs(desiredXVelocity) > 0.1f;
|
||||||
TryJump(isMoving);
|
TryJump(isMoving, desiredXVelocity);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Transform FindNearestTreat()
|
private Transform FindNearestTreat()
|
||||||
@@ -219,6 +228,8 @@ public class TrickController : MonoBehaviour
|
|||||||
}
|
}
|
||||||
|
|
||||||
var spawnPosition = hit.transform.position;
|
var spawnPosition = hit.transform.position;
|
||||||
|
hit.enabled = false;
|
||||||
|
hit.gameObject.tag = "Untagged";
|
||||||
Destroy(hit.gameObject);
|
Destroy(hit.gameObject);
|
||||||
if (target == hit.transform)
|
if (target == hit.transform)
|
||||||
{
|
{
|
||||||
@@ -287,41 +298,8 @@ public class TrickController : MonoBehaviour
|
|||||||
return Mathf.Sign(origin.x - closest.position.x);
|
return Mathf.Sign(origin.x - closest.position.x);
|
||||||
}
|
}
|
||||||
|
|
||||||
private float GetAvoidTrickDirection()
|
|
||||||
{
|
|
||||||
if (ActiveTricks.Count <= 1)
|
|
||||||
{
|
|
||||||
return 0f;
|
|
||||||
}
|
|
||||||
|
|
||||||
Transform closest = null;
|
private void TryJump(bool isMoving, float jumpDirection)
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
if (!isMoving || Time.time < nextJumpTime)
|
if (!isMoving || Time.time < nextJumpTime)
|
||||||
{
|
{
|
||||||
@@ -341,6 +319,12 @@ public class TrickController : MonoBehaviour
|
|||||||
|
|
||||||
var velocity = body.linearVelocity;
|
var velocity = body.linearVelocity;
|
||||||
velocity.y = 0f;
|
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.linearVelocity = velocity;
|
||||||
body.AddForce(Vector2.up * jumpImpulse, ForceMode2D.Impulse);
|
body.AddForce(Vector2.up * jumpImpulse, ForceMode2D.Impulse);
|
||||||
nextJumpTime = Time.time + jumpCooldown;
|
nextJumpTime = Time.time + jumpCooldown;
|
||||||
@@ -377,9 +361,32 @@ public class TrickController : MonoBehaviour
|
|||||||
if (carried)
|
if (carried)
|
||||||
{
|
{
|
||||||
target = null;
|
target = null;
|
||||||
|
desiredXVelocity = 0f;
|
||||||
var velocity = body.linearVelocity;
|
var velocity = body.linearVelocity;
|
||||||
velocity.x = 0f;
|
velocity.x = 0f;
|
||||||
|
velocity.y = 0f;
|
||||||
body.linearVelocity = velocity;
|
body.linearVelocity = velocity;
|
||||||
|
nextJumpTime = Time.time + 0.1f;
|
||||||
|
if (TryGetComponent<Collider2D>(out var collider))
|
||||||
|
{
|
||||||
|
collider.enabled = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var velocity = body.linearVelocity;
|
||||||
|
velocity.x = 0f;
|
||||||
|
velocity.y = 0f;
|
||||||
|
body.linearVelocity = velocity;
|
||||||
|
if (TryGetComponent<Collider2D>(out var collider))
|
||||||
|
{
|
||||||
|
collider.enabled = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void OnThrown()
|
||||||
|
{
|
||||||
|
movementLockUntil = Time.time + throwControlLockTime;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 4eb1753179e848ed8662b6f039a33df9
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
Reference in New Issue
Block a user