# How to align to 2D ground surface directly underneath?

I'm out of ideas at this point. I'm trying to set up a function inside my player controller that aligns the player with whatever is under it (usually the ground), ive tried using Raycasts, collisions and overlaps but it all doesn't seem to work.

First of all, I tried using OverlapCircle to detect what object the player is standing on, then it if identifies an object (so if you're standing on something) it rotates the player to the object's rotation. This worked okay but there were some big problems:

• When you stand on an intersection with more than one object, you start jittering between both objects rotations.
• When I finish the concept stage of my game, and start adding assets, most assets won't be rotated but will still have curves in them. Meaning it will try to rotate to the asset's rotation, but it won't be.
• Sort of linking into the last, curved objects don't work, since objects can't have multiple rotations throughout, so trying to make curves doesn't work. Meaning I have to set every one individually

So next I tried RayCasting but that had roughly the same issues. To fix the sporadic rotation jitter, I tried using Physics2D.RayCastAll() and then breaking after the first one like this:

foreach (RaycastHit2D rc2d in rc) {
if (rc2d) {
if (!(rc2d.transform.rotation.z < -maxAngle || rc2d.transform.rotation.z > maxAngle))
{
transform.rotation = rc2d.transform.rotation;
}
else {
transform.rotation = Quaternion.identity;
}
}
Debug.Log(rc2d.transform.name);
if (rc2d.transform.gameObject.CompareTag("Slope")) {break;}
}


Because I'm pretty sure RayCast adds to the array in the order it finds it, it would set the first object in the array to the closed object, and then after I set the rotation, just break the foreach to stop it rotating to the other objects. This kinda worked, but I still don't see how to fix the issues above.

I also tried setting the rotation after colliding with an object, but this just broke. It also meant touching anything would just rotate to it which obviously isn't a viable solution.

No one really seems to have done this before, even though a lot of 2D games have ground alignment, so my question is how are other people doing this, is it worth the effort, and how do I actually get the ground rotation?

I'm using Unity 2022.3.13f1 in a 2D project.

• You may be interested in How do I make a character capable of walking on walls? Even if you don't want to literally climb up walls, the the technique of firing two rays and averaging between them helps smooth over the jumps or jitters you describe at the junctions between surfaces. Commented Jan 15 at 12:01
• appreciate the edit, normally edge picks up on spelling mistakes but clearly it isn't very good. The alignment method you suggested looks like it could work, is it possible to get an explanation for how to do it? It would also be useful to have a maximum angle that prevents it from rotating too much.
– Pow
Commented Jan 15 at 12:37
• The linked answer contains an explanation, and links to two articles that give example code. Is there a particular part of the explanation you need help with? Commented Jan 15 at 12:42