seblague / 2dplatformer-tutorial Goto Github PK
View Code? Open in Web Editor NEWA 2D Platform Controller in Unity
Home Page: https://www.youtube.com/playlist?list=PLFt_AvWsXl0f0hqURlhyIoAabKPgRsqjz
License: MIT License
A 2D Platform Controller in Unity
Home Page: https://www.youtube.com/playlist?list=PLFt_AvWsXl0f0hqURlhyIoAabKPgRsqjz
License: MIT License
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
Unity 5.0.2f1 and 5.1.2f1 throws: "error CS1501: No overload for method Move' takes
2' arguments" when directly copy and pasting PlatformerController.cs from the source files.
Worked about 2 weeks ago, possibly caused by some new Unity update?
Hi Sebastian,
Thank you so much for the tutorials, I have found an issue in my code which I believe is an issue in the base code but has no direct impact on the platformer functionality.
Using Mathf.SmoothDamp for velocity x with a fixed float as you do in the code seems to keep it looping between -10 and 10, it never resets to 0 unless you jump and land. I discovered this as I had implemented some code to not start climbing a wall unless a player pressed against that wall.
I have worked around the issue by using inputs for my wall climb detection, however I can't seem to figure out how to fix the smoothdamp. Setting Time.deltatime instead of the accelerationTime just results in immediate stopping. I'll submit a pull request if I ever figure it out
Thank you for your brilliant tutorial, but I've found a problem after watching and practicing the episode 5, it is that if the player stop on a slope after climbing, it may slip a bit after a while, especially when the angle of the slope is really large.
the first picture shows the raycast of the player when it just finished climbing
the second picture shows the raycast of of the player after a while
I think it may caused by the gravity? Because the gravity applies on the player every frame.
So have you found this problem either? Or maybe there is something wrong with my code?I use Unity version 5, too
Thanks.
Sorry about my poor English XD.
First off, great tutorials!
Has anyone noticed any issues when transitioning between slopes and the collisions
data? Specifically the status of collisions.below
?
Using my own copy and the source code from the repo produces the same following results (watch the output of the Below box):
Notice the quick switch between true/false? It's made it somewhat unreliable to use any animation states between grounded to falling. Any help is appreciated.
Hi there not too sure at what point I went wrong but for some reason my sprite still accumulates gravity which doesnt allow it to jump and falls off the platform really quick.
Could anyone help me on this matter?
please
Hi i will like to make the character ledge on a moving platform. I have tried but to only thing i have is on a static platform.
When the platform has to move horizontally to reach the next waypoint it goes in the opposite direction and you get this error : transform.position assign attempt for 'MovingPlatform' is not valid. Input position is { -Infinity, 6.374002, 0.000000 }.
Moving vertically is no problem . Cyclic or not also doesn't seem to matter.
I didn't have this problem after finishing the tutorial but rather a few months later when opening the project again.
There is an issue with using the bounds of a BoxCollider2D outside of the fixed update loop.
From the tests I have done the bounds may not always be relative to the player position in the update loop. This means the raycasts which use the bounds position may return no collision when they should be.
The issue seems to lie in VerticalCollisions in the Controller2D script.
When the player inputs Horizontal movement while on an ascending platform, the VerticalCollisions function does not seem to add the expected velocity.y. I've been able to figure out that the first "if (hit) {}" of the function only evaluates to "true" some of the time while the player is on ascending platforms. Other than that I'm unsure of what exactly is causing the problem.
I've implemented this code from scratch without Unity, and it works great, though I'm having an issue with the collision.
If I align my player against a box and jump and slightly tap towards the box at the right time, I'm able to get past the ray casts and enter through the box.
In the recordings above, I have 4 rays on top and bottom, and 8 on either side.
When climbing a slope and running into an overhang slope that should stop the player, the player will slowly climb the overhang until it gets too far from the slope it thinks it's climbing and falls.
In the gif I'm only holding left.
The slopes are -70 and 70.
Max Slope Angle is 80
Besides those nothing is changed from the E13 project.
Hi @SebLague,
I would like to continue working on this library.
I already fixed some bugs, and added some test-scenes, a bit of IA... ladders...
Do you feel in the mood to help?
My "Fork" is at: https://github.com/llafuente/unity-platformer
If you want to take the lead, I don't have any problems to transfer the repo to you.
Thanks for your work!
The player jumps much lower when the frame rate is limited compared to when it's unlimited.
How can I ensure the jump height does not change based on the frame rate?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.