Skip to main content
Don't repeat tags in title
Source Link
DMGregory
  • 141k
  • 23
  • 258
  • 401

How to align to object2D ground surface directly underneath - Unity?

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

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 objectsobject'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 wontwon't be rotated but will still have curves in them. Meaning it will try to rotate to the assetsasset's rotation, but it wontwon't be.
  • Sort of linking into the last, curved objects dontdon't work, since objects cantcan't have multiple rotations throughout, so trying to make curves doesn't work. Meaning I have to set every one individuallyset every one individually

So next I tried RayCasting but that had roughly the same issues. To fix the sporadic rotation jitter, iI 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 imI'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 iI still dontdon'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 2d2D 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? Any help is appreciated.

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

How to align to object directly underneath - Unity

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 objects 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 wont be rotated but will still have curves in them. Meaning it will try to rotate to the assets rotation, but it wont be.
  • Sort of linking into the last, curved objects dont work, since objects cant have multiple rotations throughout, so trying to make curves doesn't work. Meaning I have to set every one individuallyset 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 im 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 dont 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? Any help is appreciated.

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

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 individuallyset 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.

Source Link
Pow
  • 449
  • 1
  • 2
  • 12

How to align to object directly underneath - Unity

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 objects 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 wont be rotated but will still have curves in them. Meaning it will try to rotate to the assets rotation, but it wont be.
  • Sort of linking into the last, curved objects dont work, since objects cant have multiple rotations throughout, so trying to make curves doesn't work. Meaning I have to set every one individuallyset 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 im 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 dont 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? Any help is appreciated.

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