Fake Physics on Player
This commit is contained in:
@@ -16,6 +16,9 @@ namespace Input
|
||||
private Vector2 lastHoldPosition;
|
||||
private Coroutine pathfindingDragCoroutine;
|
||||
private float pathfindingDragUpdateInterval = 0.1f; // Interval in seconds
|
||||
[Header("Collision Simulation")]
|
||||
public LayerMask obstacleMask;
|
||||
public float colliderRadius = 0.5f;
|
||||
|
||||
// --- Unity/Component References ---
|
||||
private AIPath aiPath;
|
||||
@@ -153,21 +156,64 @@ namespace Input
|
||||
/// </summary>
|
||||
private void MoveDirectlyTo(Vector2 worldPosition)
|
||||
{
|
||||
if (aiPath == null) return;
|
||||
if (aiPath == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Vector3 current = transform.position;
|
||||
Vector3 target = new Vector3(worldPosition.x, worldPosition.y, current.z);
|
||||
Vector3 toTarget = (target - current);
|
||||
Vector3 direction = toTarget.normalized;
|
||||
float maxSpeed = aiPath.maxSpeed;
|
||||
float acceleration = aiPath.maxAcceleration;
|
||||
directMoveVelocity =
|
||||
Vector3.MoveTowards(directMoveVelocity, direction * maxSpeed, acceleration * Time.deltaTime);
|
||||
directMoveVelocity = Vector3.MoveTowards(directMoveVelocity, direction * maxSpeed, acceleration * Time.deltaTime);
|
||||
if (directMoveVelocity.magnitude > maxSpeed)
|
||||
{
|
||||
directMoveVelocity = directMoveVelocity.normalized * maxSpeed;
|
||||
}
|
||||
Vector3 move = directMoveVelocity * Time.deltaTime;
|
||||
if (move.magnitude > toTarget.magnitude)
|
||||
{
|
||||
move = toTarget;
|
||||
transform.position += move;
|
||||
}
|
||||
// --- Collision simulation ---
|
||||
Vector3 adjustedVelocity = AdjustVelocityForObstacles(current, directMoveVelocity);
|
||||
Vector3 adjustedMove = adjustedVelocity * Time.deltaTime;
|
||||
if (adjustedMove.magnitude > toTarget.magnitude)
|
||||
{
|
||||
adjustedMove = toTarget;
|
||||
}
|
||||
transform.position += adjustedMove;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Simulates collision with obstacles by raycasting in the direction of velocity and projecting the velocity if a collision is detected.
|
||||
/// </summary>
|
||||
/// <param name="position">Player's current position.</param>
|
||||
/// <param name="velocity">Intended velocity for this frame.</param>
|
||||
/// <returns>Adjusted velocity after collision simulation.</returns>
|
||||
private Vector3 AdjustVelocityForObstacles(Vector3 position, Vector3 velocity)
|
||||
{
|
||||
if (velocity.sqrMagnitude < 0.0001f)
|
||||
return velocity;
|
||||
float moveDistance = velocity.magnitude * Time.deltaTime;
|
||||
Vector2 origin = new Vector2(position.x, position.y);
|
||||
Vector2 dir = velocity.normalized;
|
||||
float rayLength = colliderRadius + moveDistance;
|
||||
RaycastHit2D hit = Physics2D.Raycast(origin, dir, rayLength, obstacleMask);
|
||||
Debug.DrawLine(origin, origin + dir * rayLength, Color.red, 0.1f);
|
||||
if (hit.collider != null)
|
||||
{
|
||||
// Draw normal and tangent for debug
|
||||
Debug.DrawLine(hit.point, hit.point + hit.normal, Color.green, 0.2f);
|
||||
Vector2 tangent = new Vector2(-hit.normal.y, hit.normal.x);
|
||||
Debug.DrawLine(hit.point, hit.point + tangent, Color.blue, 0.2f);
|
||||
// Project velocity onto tangent to simulate sliding
|
||||
float slideAmount = Vector2.Dot(velocity, tangent);
|
||||
Vector3 slideVelocity = tangent * slideAmount;
|
||||
return slideVelocity;
|
||||
}
|
||||
return velocity;
|
||||
}
|
||||
|
||||
void Update()
|
||||
|
||||
Reference in New Issue
Block a user