-
Notifications
You must be signed in to change notification settings - Fork 251
Description
Hi guys,
There was an issue I had where going over the edge of a peak on an angle platform causes you to jitter as it figures out where the collision occurs. As one ray goes over the edge the player drops until a suitable collision occurs as shown in this image

I set out to solve this without increasing the number of rays cast down and decided on a compromise that isn't exactly elegant but it largely solves the problem.
My idea was that it only occurs in a small area so I could cast a ray horizontally across the bottom of the player then use the collision from that to cast one more ray downwards to create collisions. This adds two more ray calculations and makes far smoother situations in this case.
The following code was added to the Controller2D.cs script, at the top of the VerticalCollisions method.
Note that I also took the creation of rayOrigin and hit out of the for loop.
`
Vector2 rayOrigin = raycastOrigins.bottomLeft + new Vector2(skinWidth, -skinWidth);
float horRayLength = Vector2.Distance(raycastOrigins.bottomRight, raycastOrigins.bottomLeft) - (skinWidth * 2);
RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.right, horRayLength, collisionMask);
if (directionY == -1)
{
if (hit && hit.collider.tag != "Through")
{
Debug.DrawRay(raycastOrigins.bottomLeft, new Vector2(horRayLength, 0), Color.green);
rayOrigin = new Vector2(hit.point.x, raycastOrigins.bottomLeft.y);
hit = Physics2D.Raycast(rayOrigin, Vector2.down, rayLength, collisionMask);
Debug.DrawRay(rayOrigin, Vector2.down, Color.green);
if (hit)
{
moveAmount.y = (hit.distance - skinWidth) * -1;
rayLength = hit.distance;
if (collisions.climbingSlope)
{
moveAmount.x = moveAmount.y / Mathf.Tan(collisions.slopeAngle * Mathf.Deg2Rad) * Mathf.Sign(moveAmount.x);
}
collisions.below = true;
}
}
}
`
If anybody has a cleaner solution to this or needs it explained better, let me know
