Trick ehancements
This commit is contained in:
@@ -25,14 +25,20 @@ public class TrickController : MonoBehaviour
|
||||
[SerializeField] private float wanderWeight = 0.4f;
|
||||
[SerializeField] private int maxTricks = 50;
|
||||
[SerializeField] private float jumpImpulse = 6f;
|
||||
[SerializeField] private float jumpImpulseVariance = 0.25f;
|
||||
[SerializeField] private float jumpCooldown = 1f;
|
||||
[SerializeField] private float jumpChancePerSecond = 0.5f;
|
||||
[SerializeField] private float jumpArcChance = 0.5f;
|
||||
[SerializeField] private float jumpArcHorizontalImpulse = 2f;
|
||||
[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;
|
||||
[SerializeField] private float despawnMaxY = 20f;
|
||||
[SerializeField] private float throwControlLockTime = 0.35f;
|
||||
[SerializeField] private float wallCheckDistance = 0.1f;
|
||||
[SerializeField] private float wallAvoidDuration = 0.6f;
|
||||
|
||||
private Rigidbody2D body;
|
||||
private Transform target;
|
||||
@@ -44,6 +50,10 @@ public class TrickController : MonoBehaviour
|
||||
private float desiredXVelocity;
|
||||
private float nextJumpTime;
|
||||
private float movementLockUntil;
|
||||
private readonly RaycastHit2D[] groundHits = new RaycastHit2D[4];
|
||||
private readonly RaycastHit2D[] wallHits = new RaycastHit2D[4];
|
||||
private float wallAvoidUntil;
|
||||
private float wallAvoidDirection;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
@@ -79,7 +89,7 @@ public class TrickController : MonoBehaviour
|
||||
}
|
||||
|
||||
var position = transform.position;
|
||||
if (position.x < despawnMinX || position.x > despawnMaxX || position.y < despawnMinY)
|
||||
if (position.x < despawnMinX || position.x > despawnMaxX || position.y < despawnMinY || position.y > despawnMaxY)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
@@ -128,9 +138,6 @@ public class TrickController : MonoBehaviour
|
||||
|
||||
if (!IsGrounded())
|
||||
{
|
||||
var airborne = body.linearVelocity;
|
||||
airborne.x = 0f;
|
||||
body.linearVelocity = airborne;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -156,6 +163,18 @@ public class TrickController : MonoBehaviour
|
||||
desiredX = wanderDirection.x * 0.2f;
|
||||
}
|
||||
|
||||
var wallDirection = GetWallAvoidDirection(desiredX);
|
||||
if (wallDirection != 0f)
|
||||
{
|
||||
wallAvoidDirection = wallDirection;
|
||||
wallAvoidUntil = Time.time + wallAvoidDuration;
|
||||
}
|
||||
|
||||
if (Time.time < wallAvoidUntil && Mathf.Abs(wallAvoidDirection) > 0.01f)
|
||||
{
|
||||
desiredX = wallAvoidDirection;
|
||||
}
|
||||
|
||||
desiredVelocity = new Vector2(Mathf.Clamp(desiredX, -1f, 1f) * moveSpeed, 0f);
|
||||
|
||||
var velocity = body.linearVelocity;
|
||||
@@ -311,6 +330,11 @@ public class TrickController : MonoBehaviour
|
||||
return;
|
||||
}
|
||||
|
||||
if (body.linearVelocity.y > 0.01f)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var chance = jumpChancePerSecond * Time.fixedDeltaTime;
|
||||
if (Random.value > chance)
|
||||
{
|
||||
@@ -326,7 +350,17 @@ public class TrickController : MonoBehaviour
|
||||
}
|
||||
velocity.x = jumpDir * moveSpeed;
|
||||
body.linearVelocity = velocity;
|
||||
body.AddForce(Vector2.up * jumpImpulse, ForceMode2D.Impulse);
|
||||
|
||||
var minImpulse = Mathf.Max(0f, jumpImpulse * (1f - jumpImpulseVariance));
|
||||
var maxImpulse = Mathf.Max(minImpulse, jumpImpulse * (1f + jumpImpulseVariance));
|
||||
var jumpImpulseActual = Random.Range(minImpulse, maxImpulse);
|
||||
body.AddForce(Vector2.up * jumpImpulseActual, ForceMode2D.Impulse);
|
||||
|
||||
if (Random.value < jumpArcChance && jumpArcHorizontalImpulse > 0f)
|
||||
{
|
||||
var arcDir = Random.value < 0.5f ? -1f : 1f;
|
||||
body.AddForce(Vector2.right * arcDir * jumpArcHorizontalImpulse, ForceMode2D.Impulse);
|
||||
}
|
||||
nextJumpTime = Time.time + jumpCooldown;
|
||||
}
|
||||
|
||||
@@ -338,22 +372,78 @@ public class TrickController : MonoBehaviour
|
||||
return false;
|
||||
}
|
||||
|
||||
var bounds = collider.bounds;
|
||||
var checkSize = new Vector2(bounds.size.x * 0.9f, groundCheckDistance * 2f);
|
||||
var checkCenter = new Vector2(bounds.center.x, bounds.min.y - groundCheckDistance);
|
||||
var hits = Physics2D.OverlapBoxAll(checkCenter, checkSize, 0f, groundMask);
|
||||
for (var i = 0; i < hits.Length; i++)
|
||||
var filter = new ContactFilter2D();
|
||||
filter.SetLayerMask(groundMask);
|
||||
filter.useTriggers = false;
|
||||
|
||||
var hitCount = collider.Cast(Vector2.down, filter, groundHits, groundCheckDistance);
|
||||
for (var i = 0; i < hitCount; i++)
|
||||
{
|
||||
var hit = hits[i];
|
||||
if (hit != null && hit != collider)
|
||||
var hitCollider = groundHits[i].collider;
|
||||
if (hitCollider == null || hitCollider == collider)
|
||||
{
|
||||
return true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (hitCollider.GetComponent<TrickController>() != null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (hitCollider.GetComponent<PlayerPlatformerController>() != null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private float GetWallAvoidDirection(float desiredX)
|
||||
{
|
||||
if (Mathf.Abs(desiredX) < 0.01f)
|
||||
{
|
||||
return 0f;
|
||||
}
|
||||
|
||||
var collider = GetComponent<Collider2D>();
|
||||
if (collider == null)
|
||||
{
|
||||
return 0f;
|
||||
}
|
||||
|
||||
var filter = new ContactFilter2D();
|
||||
filter.SetLayerMask(groundMask);
|
||||
filter.useTriggers = false;
|
||||
|
||||
var dir = Mathf.Sign(desiredX);
|
||||
var hitCount = collider.Cast(Vector2.right * dir, filter, wallHits, wallCheckDistance);
|
||||
for (var i = 0; i < hitCount; i++)
|
||||
{
|
||||
var hitCollider = wallHits[i].collider;
|
||||
if (hitCollider == null || hitCollider == collider)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (hitCollider.GetComponent<TrickController>() != null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (hitCollider.GetComponent<PlayerPlatformerController>() != null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
return -dir;
|
||||
}
|
||||
|
||||
return 0f;
|
||||
}
|
||||
|
||||
|
||||
public void SetCarried(bool carried)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user