Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected void Start()
- {
- contactFilter.useTriggers = false;
- contactFilter.SetLayerMask(Physics2D.GetLayerCollisionMask(gameObject.layer));
- contactFilter.useLayerMask = true;
- }
- protected void FixedUpdate()
- {
- velocity += gravityModifier * Physics2D.gravity * Time.deltaTime;
- velocity.x = targetVelocity.x + impulse.x;
- if (impulse.x > 0) impulse.x = Mathf.Max(0, impulse.x - 10 * Time.deltaTime);
- else if (impulse.x < 0) impulse.x = Mathf.Min(0, impulse.x + 10 * Time.deltaTime);
- grounded = false;
- Vector2 moveAlongGround = new Vector2(groundNormal.y, -groundNormal.x);
- Vector2 deltaPosition = velocity * Time.deltaTime;
- Vector2 move = moveAlongGround * deltaPosition;
- Movement(move, false);
- move = Vector2.up * deltaPosition.y;
- Movement(move, true);
- }
- void Movement(Vector2 move, bool yMovement)
- {
- float distance = move.magnitude;
- if (distance > minMoveDistance)
- {
- int count = rb2d.Cast(move, contactFilter, hitBuffer, distance + shellRadius);
- hitBufferList.Clear();
- for (int i = 0; i < count; i++)
- {
- hitBufferList.Add(hitBuffer[i]);
- }
- for (int i = 0; i < hitBufferList.Count; i++)
- {
- Vector2 currentNormal = hitBufferList[i].normal;
- if (currentNormal.y > minGroundNormalY)
- {
- grounded = true;
- if (yMovement)
- {
- groundNormal = currentNormal;
- currentNormal.x = 0;
- }
- }
- float projection = Vector2.Dot(velocity, currentNormal);
- if (projection < 0)
- {
- velocity = velocity - projection * currentNormal;
- }
- float modifiedDistance = hitBufferList[i].distance - shellRadius;
- distance = modifiedDistance < distance ? modifiedDistance : distance;
- }
- }
- if (distance > 0.01F) rb2d.position += move.normalized * distance;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement