daily pastebin goal
83%
SHARE
TWEET

Untitled

a guest Oct 14th, 2014 523 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.        
  2.  
  3.                     origin = charHead.position;
  4.                     Vector3 occRay = transform.position - origin;
  5.                     float thinRadius = 0.15f;
  6.                     float thickRadius = 0.3f;
  7.                    
  8.                     var colPoint = GetCollisionSimple(transform.position, thinRadius, true);
  9.                     var colPointThick = GetCollisionSimple(transform.position, thickRadius, false);
  10.                    
  11.                     var colPointThickProjectedOnRay = Vector3.Project(colPointThick - origin, occRay.normalized) + origin;
  12.                     var vecToProjected = (colPointThickProjectedOnRay - colPointThick).normalized;
  13.                     var colPointThickProjectedOnThinCapsule = colPointThickProjectedOnRay - vecToProjected * thinRadius;
  14.                     var thin2ThickDist = Vector3.Distance(colPointThickProjectedOnThinCapsule, colPointThick);
  15.                     var thin2ThickDistNorm = thin2ThickDist / (thickRadius - thinRadius);
  16.                    
  17.                     float currentColDist = Vector3.Distance(origin, colPoint);
  18.                     float currentColDistThick = Vector3.Distance(origin, colPointThickProjectedOnRay);
  19.                     currentColDist = Mathf.Lerp(currentColDistThick, currentColDist, thin2ThickDistNorm);
  20.                    
  21.                     colDist = currentColDist < colDist? currentColDist : Mathf.SmoothStep(colDist, currentColDist, Time.deltaTime*100);
  22.                     colDist = Mathf.Min(colDist, 2);
  23.                     vec = transform.position - origin;
  24.                     transform.position = origin + vec.normalized * colDist;
  25.                    
  26.                    
  27.                    
  28.                    
  29.             Vector3 GetCollisionSimple(Vector3 cameraOptPos, float radius, bool pushByNormal)
  30.             {
  31.                     float farEnough = 1;
  32.            
  33.                     RaycastHit occHit;
  34.                     Vector3 origin = charHead.position;
  35.                     Vector3 occRay = origin - cameraOptPos;
  36.                     float dt = Vector3.Dot(transform.forward, occRay);
  37.                     if (dt < 0) occRay *= -1;
  38.                    
  39.                     if (Physics.SphereCast(origin, radius, occRay.normalized, out occHit, farEnough, mask))
  40.                     {
  41.                             origin = origin + occRay.normalized * occHit.distance;
  42.                     }
  43.                     else
  44.                     {
  45.                             origin += occRay.normalized * farEnough;
  46.                     }
  47.                    
  48.                    
  49.                     occRay = origin - cameraOptPos;
  50.                     if (Physics.SphereCast(origin, radius, -occRay.normalized, out occHit, occRay.magnitude, mask))
  51.                     {
  52.                             return pushByNormal? occHit.point + occHit.normal*radius : occHit.point;
  53.                     }
  54.                     else
  55.                     {
  56.                             return cameraOptPos;
  57.                     }
  58.             }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top