Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. protected void Start()
  2. {
  3. contactFilter.useTriggers = false;
  4. contactFilter.SetLayerMask(Physics2D.GetLayerCollisionMask(gameObject.layer));
  5. contactFilter.useLayerMask = true;
  6. }
  7.  
  8. protected void FixedUpdate()
  9. {
  10. velocity += gravityModifier * Physics2D.gravity * Time.deltaTime;
  11. velocity.x = targetVelocity.x + impulse.x;
  12.  
  13. if (impulse.x > 0) impulse.x = Mathf.Max(0, impulse.x - 10 * Time.deltaTime);
  14. else if (impulse.x < 0) impulse.x = Mathf.Min(0, impulse.x + 10 * Time.deltaTime);
  15.  
  16. grounded = false;
  17.  
  18. Vector2 moveAlongGround = new Vector2(groundNormal.y, -groundNormal.x);
  19. Vector2 deltaPosition = velocity * Time.deltaTime;
  20.  
  21. Vector2 move = moveAlongGround * deltaPosition;
  22. Movement(move, false);
  23.  
  24. move = Vector2.up * deltaPosition.y;
  25. Movement(move, true);
  26. }
  27.  
  28. void Movement(Vector2 move, bool yMovement)
  29. {
  30. float distance = move.magnitude;
  31.  
  32. if (distance > minMoveDistance)
  33. {
  34. int count = rb2d.Cast(move, contactFilter, hitBuffer, distance + shellRadius);
  35. hitBufferList.Clear();
  36.  
  37. for (int i = 0; i < count; i++)
  38. {
  39. hitBufferList.Add(hitBuffer[i]);
  40. }
  41.  
  42. for (int i = 0; i < hitBufferList.Count; i++)
  43. {
  44. Vector2 currentNormal = hitBufferList[i].normal;
  45.  
  46. if (currentNormal.y > minGroundNormalY)
  47. {
  48. grounded = true;
  49. if (yMovement)
  50. {
  51. groundNormal = currentNormal;
  52. currentNormal.x = 0;
  53. }
  54. }
  55.  
  56. float projection = Vector2.Dot(velocity, currentNormal);
  57.  
  58. if (projection < 0)
  59. {
  60. velocity = velocity - projection * currentNormal;
  61. }
  62.  
  63.  
  64. float modifiedDistance = hitBufferList[i].distance - shellRadius;
  65. distance = modifiedDistance < distance ? modifiedDistance : distance;
  66. }
  67. }
  68.  
  69. if (distance > 0.01F) rb2d.position += move.normalized * distance;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement