SHARE
TWEET

Untitled

a guest Jul 23rd, 2015 115 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  void CameraCollision()
  2.         {
  3.                 var camPivot = Control.Active.p.head;
  4.                 transform.position = transform.parent.position;
  5.                 Vector3 vec;
  6.                
  7.                 Vector3 normal, thickNormal;
  8.                 float maxColDist = 2;
  9.                 var origin = camPivot.position;
  10.                         origin += Vector3.up * charHeadOffset;//1.897082f;
  11.                 Vector3 occRay = transform.position - origin;
  12.                 float thinRadius = 0.15f;
  13.                 float thickRadius = 0.3f;
  14.                
  15.                 colPoint = GetCollisionSimple(camPivot, transform.position, thinRadius, out normal, true);
  16.                 colPointThick = GetCollisionSimple(camPivot, transform.position, thickRadius, out thickNormal, false);
  17.                 colPointRay = GetCollision(camPivot, transform.position);
  18.                
  19.                 var colPointThickProjectedOnRay = Vector3.Project(colPointThick - origin, occRay.normalized) + origin;
  20.                 var vecToProjected = (colPointThickProjectedOnRay - colPointThick).normalized;
  21.                 var colPointThickProjectedOnThinCapsule = colPointThickProjectedOnRay - vecToProjected * thinRadius;
  22.                 var thin2ThickDist = Vector3.Distance(colPointThickProjectedOnThinCapsule, colPointThick);
  23.                 var thin2ThickDistNorm = thin2ThickDist / (thickRadius - thinRadius);
  24.  
  25.                 float currentColDistThin = Vector3.Distance(origin, colPoint);
  26.                 float currentColDistThick = Vector3.Distance(origin, colPointThickProjectedOnRay);
  27.                 float currentColDist = Mathf.Lerp(currentColDistThick, currentColDistThin, thin2ThickDistNorm);
  28.  
  29.                 // Thick point can be actually projected IN FRONT of the character due to double projection to avoid sphere moving through the walls
  30.                 // In this case we should only use thin point
  31.                 var isThickPointIncorrect = transform.InverseTransformDirection(colPointThick - origin).z > 0;
  32.                 isThickPointIncorrect = isThickPointIncorrect || (currentColDistThin < currentColDistThick);
  33.                 if (isThickPointIncorrect) currentColDist = currentColDistThin;
  34.                
  35.                 colDist = currentColDist < colDist? currentColDist : Mathf.SmoothStep(colDist, currentColDist, Time.deltaTime * 100 * Mathf.Max(colDist*0.1f, 0.1f));
  36.                 colDist = Mathf.Min(colDist, maxColDist);
  37.                 vec = transform.position - origin;
  38.                 transform.position = origin + vec.normalized * colDist;
  39.                
  40.                 if (Vector3.Distance(origin, colPoint) > Vector3.Distance(origin, colPointRay)) transform.position = colPointRay;
  41.                 transform.position += Vector3.up * Mathf.Lerp(0.1f, 0, saturate(colDist/0.5f));
  42.         }
  43.  
  44.         float saturate(float a)
  45.         {
  46.                 if (a<0) return 0;
  47.                 if (a>1) return 1;
  48.                 return a;
  49.         }
  50.        
  51.         Vector3 GetCollisionSimple(Transform camPivot, Vector3 cameraOptPos, float radius, out Vector3 normal, bool pushByNormal)
  52.         {
  53.                 float farEnough = 1;
  54.        
  55.                 RaycastHit occHit;
  56.                 Vector3 origin = camPivot.position;
  57.                         origin += Vector3.up * charHeadOffset;
  58.                 Vector3 occRay = origin - cameraOptPos;
  59.                 float dt = Vector3.Dot(transform.forward, occRay);
  60.                 if (dt < 0) occRay *= -1;
  61.                
  62.                 // Project the sphere in an opposite direction of the desired character->camera vector to get some space for the real spherecast
  63.                 if (Physics.SphereCast(origin, radius, occRay.normalized, out occHit, farEnough, mask))
  64.                 {
  65.                         origin = origin + occRay.normalized * occHit.distance;
  66.                 }
  67.                 else
  68.                 {
  69.                         origin += occRay.normalized * farEnough;
  70.                 }
  71.                
  72.                 // Do final spherecast with offset origin
  73.                 occRay = origin - cameraOptPos;
  74.                 if (Physics.SphereCast(origin, radius, -occRay.normalized, out occHit, occRay.magnitude, mask))
  75.                 {
  76.                         normal = occHit.normal;                
  77.                         return pushByNormal? occHit.point + occHit.normal*radius : occHit.point;
  78.                 }
  79.                 else
  80.                 {
  81.                         normal = Vector3.zero;
  82.                         return cameraOptPos;
  83.                 }
  84.         }
  85.        
  86.         Vector3 GetCollision(Transform camPivot, Vector3 cameraOptPos)
  87.         {
  88.                 Vector3 origin = camPivot.position;
  89.                         origin += Vector3.up * charHeadOffset;//1.897082f;
  90.                 Vector3 occRay = cameraOptPos - origin;
  91.                
  92.                 RaycastHit hit;
  93.                 if (Physics.Raycast(origin, occRay.normalized, out hit, occRay.magnitude, mask))
  94.                 {
  95.                         return hit.point + hit.normal * 0.15f;
  96.                 }
  97.                
  98.                 return cameraOptPos;
  99.         }
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