Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Help with rotation snapping to surface & cleaning up of some bad code

Discussion in 'Getting Started' started by BasicDeveloper_100, Apr 10, 2024.

  1. BasicDeveloper_100

    BasicDeveloper_100

    Joined:
    Jun 28, 2023
    Posts:
    12
    I am making a platformer partially inspired by 'Geometry Dash', where the player can switch between 'forms' by pressing certain keys. Each form has different physics statistics and some special abilities. Right now, my problem is making the rotation of the player snap to the surface below. I have some code which is probably inefficient and it mostly works but there is one specific problem. The BoxCast sometimes rotates the player when it gets close to a sloped object but not close enough to the point where it is touching it. This is my only problem. Also, I have a feeling that my code may be very inefficient in some places. For example, I use a full if else statement to find if the player is on a tip which was glitching out and that was the only way I could fix it.

    I need an explanation and possibly a snippet of code for this because I am a bit of a beginner to code. I will learn how this all works when I get the working code, though.

    My 51 line abomination:
    Code (CSharp):
    1. private void Rotate()
    2.     {
    3.         bool isOnTip = false;
    4.  
    5.         Quaternion rot = transform.rotation;
    6.         transform.rotation = Quaternion.identity;
    7.         RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, 0f, Vector2.down, boxCollider.bounds.extents.y + 10f, groundLayer);
    8.         RaycastHit2D raycastHitRight = Physics2D.Raycast(boxCollider.bounds.center + new Vector3(boxCollider.bounds.extents.x, 0), Vector2.down, boxCollider.bounds.extents.y + 0.03f, groundLayer);
    9.         RaycastHit2D raycastHitLeft = Physics2D.Raycast(boxCollider.bounds.center - new Vector3(boxCollider.bounds.extents.x, 0), Vector2.down, boxCollider.bounds.extents.y + 0.03f, groundLayer);
    10.  
    11.         Color rayColor;
    12.  
    13.         if (raycastHit.collider != null)
    14.         {
    15.             rayColor = Color.green;
    16.  
    17.         }
    18.         else
    19.         {
    20.             rayColor = Color.red;
    21.         }
    22.  
    23.         Debug.Log(raycastHitLeft.collider);
    24.         Debug.Log(raycastHitRight.collider);
    25.  
    26.         if (raycastHitRight.collider == null && raycastHitLeft.collider == null)
    27.         {
    28.             isOnTip = true;
    29.         }
    30.  
    31.         Debug.DrawRay(boxCollider.bounds.center + new Vector3(boxCollider.bounds.extents.x, 0), Vector2.down * boxCollider.bounds.extents.y, rayColor);
    32.         Debug.DrawRay(boxCollider.bounds.center - new Vector3(boxCollider.bounds.extents.x, 0), Vector2.down * boxCollider.bounds.extents.y, rayColor);
    33.  
    34.         if (raycastHit.collider != null)
    35.         {
    36.             if (isOnTip)
    37.             {
    38.                 rotation = Quaternion.identity;
    39.             }
    40.             else
    41.             {
    42.                 rotation = Quaternion.FromToRotation(Vector3.up, raycastHit.normal);
    43.             }
    44.         }
    45.         else
    46.         {
    47.             rotation = Quaternion.identity;
    48.         }
    49.  
    50.         transform.rotation = rot;
    51.     }
    Anyway, thank you in advance for any help with this.