Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Ledge grabbing messed up after Unity Update

Discussion in 'Scripting' started by PegasiStudios, Jul 9, 2019.

  1. PegasiStudios

    PegasiStudios

    Joined:
    Dec 22, 2014
    Posts:
    26
    Having troubles with the characters ledge grabbing. This is what is supposed to detect the ledge grabbing, which worked fine before, but now the player grabs onto any wall that has a floor above it? suggestions? NOTE- I updated from 2018.1 to 2019, which caused this issue.
    Code (CSharp):
    1. private bool CanGrabLedge(out Vector3 hitPosition, out GameObject grabbedObject)
    2.     {
    3.         hitPosition = Vector3.zero;
    4.  
    5.         grabbedObject = null;
    6.  
    7.         Vector3 o = controller.OffsetPosition(controller.head.Offset);
    8.  
    9.         Collider[] colliders = Physics.OverlapSphere(o, controller.radius + 0.2f, controller.Walkable);
    10.  
    11.         if (colliders.Length > 0)
    12.         {
    13.             foreach (var col in colliders)
    14.             {
    15.                 SuperCollisionType type = col.GetComponent<SuperCollisionType>();
    16.  
    17.                 Vector3 closestPoint = SuperCollider.ClosestPointOnSurface(col, o, controller.radius);
    18.  
    19.                 RaycastHit hit;
    20.  
    21.                 col.Raycast(new Ray(o, closestPoint - o), out hit, Mathf.Infinity);
    22.  
    23.                 if (Vector3.Angle(hit.normal, controller.up) < type.StandAngle)
    24.                 {
    25.                     continue;
    26.                 }
    27.  
    28.                 if (Vector3.Angle(-hit.normal, lookDirection) < 80.0f)
    29.                 {
    30.                     Vector3 topOfHead = o + controller.up * controller.radius;
    31.                     Vector3 direction = Math3d.ProjectVectorOnPlane(controller.up, closestPoint - o);
    32.                     Vector3 rayOrigin = topOfHead + Math3d.AddVectorLength(direction, 0.02f);
    33.  
    34.                     col.Raycast(new Ray(rayOrigin, controller.down), out hit, 0.5f);
    35.  
    36.                     if (Vector3.Angle(hit.normal, controller.up) < 20.0f)
    37.                     {
    38.                         hitPosition = Math3d.ProjectPointOnPlane(controller.up, hit.point, topOfHead + direction);
    39.                         grabbedObject = col.gameObject;
    40.  
    41.                         return true;
    42.                     }
    43.                 }
    44.             }
    45.         }
    46.  
    47.         return false;
    48.     }