Fixing trick behavior
This commit is contained in:
@@ -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}
|
||||
|
||||
@@ -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}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user