Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed: Jittery movement at angle platform peaks #9

Open
gatz827 opened this issue Feb 25, 2017 · 0 comments
Open

Fixed: Jittery movement at angle platform peaks #9

gatz827 opened this issue Feb 25, 2017 · 0 comments

Comments

@gatz827
Copy link

gatz827 commented Feb 25, 2017

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
jitterypeak

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.

jitterypeakfixed

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant