Fixing trick behavior
This commit is contained in:
@@ -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