Advertisement
Guest User

Untitled

a guest
Oct 7th, 2015
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. /// <summary>
  2. /// we have to use a bit of trickery in this one. The rays must be cast from a small distance inside of our
  3. /// collider (skinWidth) to avoid zero distance rays which will get the wrong normal. Because of this small offset
  4. /// we have to increase the ray distance skinWidth then remember to remove skinWidth from deltaMovement before
  5. /// actually moving the player
  6. /// </summary>
  7. private void moveHorizontally( ref Vector3 deltaMovement )
  8. {
  9. var isGoingRight = deltaMovement.x > 0;
  10. var rayDistance = Mathf.Abs( deltaMovement.x ) + _skinWidth;
  11. var rayDirection = isGoingRight ? Vector2.right : -Vector2.right;
  12. var initialRayOrigin = isGoingRight ? _raycastOrigins.bottomRight : _raycastOrigins.bottomLeft;
  13.  
  14. for( var i = 0; i < totalHorizontalRays; i++ )
  15. {
  16. var ray = new Vector2( initialRayOrigin.x, initialRayOrigin.y + i * _verticalDistanceBetweenRays );
  17.  
  18. DrawRay( ray, rayDirection * rayDistance, Color.red );
  19.  
  20. // if we are grounded we will include oneWayPlatforms only on the first ray (the bottom one). this will allow us to
  21. // walk up sloped oneWayPlatforms
  22. if( i == 0 && collisionState.wasGroundedLastFrame )
  23. _raycastHit = Physics2D.Raycast( ray, rayDirection, rayDistance, platformMask );
  24. else
  25. _raycastHit = Physics2D.Raycast( ray, rayDirection, rayDistance, platformMask & ~oneWayPlatformMask );
  26.  
  27. if( _raycastHit )
  28. {
  29. // the bottom ray can hit slopes but no other ray can so we have special handling for those cases
  30. if( i == 0 && handleHorizontalSlope( ref deltaMovement, Vector2.Angle( _raycastHit.normal, Vector2.up ) ) )
  31. {
  32. _raycastHitsThisFrame.Add( _raycastHit );
  33. break;
  34. }
  35.  
  36. // set our new deltaMovement and recalculate the rayDistance taking it into account
  37. deltaMovement.x = _raycastHit.point.x - ray.x;
  38. rayDistance = Mathf.Abs( deltaMovement.x );
  39.  
  40. // remember to remove the skinWidth from our deltaMovement
  41. if( isGoingRight )
  42. {
  43. deltaMovement.x -= _skinWidth;
  44. collisionState.right = true;
  45. }
  46. else
  47. {
  48. deltaMovement.x += _skinWidth;
  49. collisionState.left = true;
  50. }
  51.  
  52. _raycastHitsThisFrame.Add( _raycastHit );
  53.  
  54. // we add a small fudge factor for the float operations here. if our rayDistance is smaller
  55. // than the width + fudge bail out because we have a direct impact
  56. if( rayDistance < _skinWidth + kSkinWidthFloatFudgeFactor )
  57. break;
  58. }
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement