Trick ehancements
This commit is contained in:
@@ -24,6 +24,11 @@ public class PlayerPickupController : MonoBehaviour
|
||||
public float pickupRange = 2.0f;
|
||||
|
||||
public GameObject heldObject;
|
||||
private Rigidbody2D heldBody;
|
||||
private float heldBodyMass;
|
||||
private float heldBodyGravity;
|
||||
private RigidbodyType2D heldBodyType;
|
||||
private bool heldBodySimulated;
|
||||
private PlayerPlatformerController platformerController;
|
||||
private PlayerStressController playerStressController;
|
||||
private bool isChargingThrow;
|
||||
@@ -163,9 +168,17 @@ public class PlayerPickupController : MonoBehaviour
|
||||
// Disable physics so it doesn't fall or collide with the player
|
||||
if (heldObject.TryGetComponent<Rigidbody2D>(out Rigidbody2D rb))
|
||||
{
|
||||
heldBody = rb;
|
||||
heldBodyMass = rb.mass;
|
||||
heldBodyGravity = rb.gravityScale;
|
||||
heldBodyType = rb.bodyType;
|
||||
heldBodySimulated = rb.simulated;
|
||||
|
||||
rb.simulated = false;
|
||||
rb.bodyType = RigidbodyType2D.Kinematic;
|
||||
rb.gravityScale = 0;
|
||||
rb.mass = 0;
|
||||
rb.gravityScale = 0f;
|
||||
rb.linearVelocity = Vector2.zero;
|
||||
rb.angularVelocity = 0f;
|
||||
}
|
||||
|
||||
if (trick != null)
|
||||
@@ -195,11 +208,14 @@ public class PlayerPickupController : MonoBehaviour
|
||||
void DropObject(bool applyThrow)
|
||||
{
|
||||
// Re-enable physics
|
||||
if (heldObject.TryGetComponent<Rigidbody2D>(out Rigidbody2D rb))
|
||||
if (heldBody != null)
|
||||
{
|
||||
rb.bodyType = RigidbodyType2D.Dynamic;
|
||||
rb.gravityScale = 1;
|
||||
rb.linearVelocity = Vector2.zero;
|
||||
heldBody.simulated = heldBodySimulated;
|
||||
heldBody.bodyType = heldBodyType;
|
||||
heldBody.gravityScale = heldBodyGravity;
|
||||
heldBody.mass = heldBodyMass;
|
||||
heldBody.linearVelocity = Vector2.zero;
|
||||
heldBody.angularVelocity = 0f;
|
||||
}
|
||||
if (heldObject.TryGetComponent<TrickController>(out var trick))
|
||||
{
|
||||
@@ -217,7 +233,7 @@ public class PlayerPickupController : MonoBehaviour
|
||||
heldPickup.enabled = true;
|
||||
}
|
||||
}
|
||||
if (applyThrow && heldObject.TryGetComponent<Rigidbody2D>(out Rigidbody2D throwBody))
|
||||
if (applyThrow && heldBody != null)
|
||||
{
|
||||
var direction = 1f;
|
||||
if (platformerController != null && Mathf.Abs(platformerController.LastMoveX) > 0.01f)
|
||||
@@ -231,8 +247,9 @@ public class PlayerPickupController : MonoBehaviour
|
||||
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;
|
||||
var upwardSpeed = throwUpwardForce * charge01;
|
||||
heldBody.linearVelocity = new Vector2(direction * throwSpeed, upwardSpeed);
|
||||
heldBody.angularVelocity = 0f;
|
||||
}
|
||||
heldObject.transform.position += Vector3.up * dropClearance;
|
||||
Collider2D col = heldObject.GetComponent<Collider2D>();
|
||||
@@ -241,6 +258,7 @@ public class PlayerPickupController : MonoBehaviour
|
||||
// Unparent
|
||||
heldObject.transform.SetParent(null);
|
||||
heldObject = null;
|
||||
heldBody = null;
|
||||
isChargingThrow = false;
|
||||
}
|
||||
|
||||
|
||||
@@ -205,10 +205,22 @@ public class PlayerPlatformerController : MonoBehaviour
|
||||
for (var i = 0; i < hits.Length; i++)
|
||||
{
|
||||
var hit = hits[i];
|
||||
if (hit != null && hit != bodyCollider)
|
||||
if (hit == null || hit == bodyCollider || hit.isTrigger)
|
||||
{
|
||||
return true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (hit.GetComponent<PlayerPlatformerController>() != null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (hit.GetComponent<TrickController>() != null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
@@ -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