// Update is called once per frame void Update() { if (freezeController.getFreeze()) { return; } Collider2D[] results = new Collider2D[5]; ContactFilter2D filter = new ContactFilter2D(); filter.SetLayerMask(collisionMask); ctrlCollider.OverlapCollider(filter, results); for (int i = 0; i < results.Length; i++) { Collider2D hit = results[i]; if (!hit) { continue; } Hitbox hitbox = hit.GetComponent <Hitbox>(); if (!hitbox) { continue; } if (instakill) { hitbox.Die(); } else { Controller2D ctrl = hitbox.GetComponent <Controller2D>(); Vector2 direction = ctrl == null ? Vector2.right : -ctrl.collisions.faceDir * Vector2.right; if (ctrl.transform.position.y < transform.position.y) { direction.y = -1; } hitbox.Damage(damage.GetValue(), new Hitbox.Knockback { duration = knockbackDuration.GetValue(), direction = direction, speed = knockbackSpeed.GetValue() }); } } }
void CheckCollisions(ref Projectile proj) { ContactFilter2D filter = new ContactFilter2D(); filter.layerMask = hitMask | obstacleMask; Collider2D[] hits = new Collider2D[5]; proj.collider.OverlapCollider(filter, hits); List <Hitbox> hitTargets = new List <Hitbox>(hits.Length); for (int j = 0; j < hits.Length; j++) { if (hits[j] == null) { continue; } Destroy(proj.collider.gameObject); proj.destroyed = true; if (hitMask == (hitMask | 1 << hits[j].gameObject.layer)) { Hitbox hitbox = hits[j].GetComponent <Hitbox>(); if (!hitbox) { continue; } if (hitTargets.Contains(hitbox)) { continue; } if (instakill) { hitbox.Die(); } else { hitbox.Damage(damage.GetValue(), new Hitbox.Knockback { duration = knockbackDuration.GetValue(), direction = projectileDirection.x != 0 ? new Vector2(Mathf.Sign(projectileDirection.x), 0) : Vector2.zero, speed = knockbackSpeed.GetValue() }); } } } }
void DetectHit() { ContactFilter2D filter = new ContactFilter2D(); filter.layerMask = hitMask; Collider2D[] hits = new Collider2D[5]; head.OverlapCollider(filter, hits); List <Hitbox> hitTargets = new List <Hitbox>(hits.Length); for (int i = 0; i < hits.Length; i++) { if (hits[i] == null) { continue; } Hitbox hitbox = hits[i].GetComponent <Hitbox>(); if (!hitbox) { continue; } if (hitTargets.Contains(hitbox)) { continue; } if (instakill) { hitbox.Die(); } else { hitbox.Damage(damage.GetValue(), new Hitbox.Knockback { duration = knockbackDuration.GetValue(), direction = rb2d.angularVelocity > 0 ? Vector2.right : -Vector2.right, speed = knockbackSpeed.GetValue() }); } } }
// Update is called once per frame void Update() { if (freezeController.getFreeze()) { return; } Vector3 heading = transform.position - initialPosition; UpdateRaycastOrigins(); if (!retracting) { moveAmount.x = directionX * squashSpeed * Time.deltaTime; moveAmount.y = directionY * squashSpeed * Time.deltaTime; if (heading.magnitude >= squashRange) { retracting = true; } } else { moveAmount.x = -directionX * retractSpeed * Time.deltaTime; moveAmount.y = -directionY * retractSpeed * Time.deltaTime; Vector3 retractingHeading = initialPosition - initialPosition + new Vector3(directionX, directionY); if (heading.normalized != retractingHeading.normalized) { retracting = false; } } if (moveAmount.y != 0) { float dirY = Mathf.Sign(moveAmount.y); float rayLength = moveAmount.y; for (int i = 0; i < verticalRayCount; i++) { Vector2 rayOrigin = (dirY > 0 ? raycastOrigins.topLeft : raycastOrigins.bottomLeft) + Vector2.right * (verticalRaySpacing * i); RaycastHit2D hit = Physics2D.Raycast(rayOrigin, dirY * Vector2.up, rayLength, collisionMask); Debug.DrawRay(rayOrigin, dirY * Vector2.up * rayLength, Color.red); if (hit) { Controller2D hitCtrl = hit.collider.GetComponent <Controller2D>(); if (hitCtrl) { hit.collider.GetComponent <Controller2D>().Move(moveAmount, false); } if (squashRange - heading.magnitude <= hit.collider.bounds.size.y + damageRangeAroundTarget) { //Actually hit at the peak of the squash distance Hitbox hitbox = hit.collider.GetComponent <Hitbox>(); if (hitbox) { if (instakill) { hitbox.Die(); } else { hitbox.Damage(damage.GetValue(), new Hitbox.Knockback()); } } retracting = !retracting; moveAmount.y = moveAmount.y - dirY * (hit.distance - skinWidth); } break; } } } if (moveAmount.x != 0) { float dirX = Mathf.Sign(moveAmount.x); float rayLength = moveAmount.x; for (int i = 0; i < horizontalRayCount; i++) { Vector2 rayOrigin = (dirX < 0 ? raycastOrigins.bottomLeft : raycastOrigins.bottomRight) + Vector2.up * (horizontalRaySpacing * i); RaycastHit2D hit = Physics2D.Raycast(rayOrigin, dirX * Vector3.right, rayLength, collisionMask); Debug.DrawRay(rayOrigin, dirX * Vector3.right * rayLength, Color.red); if (hit) { Controller2D hitCtrl = hit.collider.GetComponent <Controller2D>(); if (hitCtrl) { hit.collider.GetComponent <Controller2D>().Move(moveAmount, false); } if (squashRange - heading.magnitude <= hit.collider.bounds.size.x + damageRangeAroundTarget) { //Actually hit at the peak of the squash distance Hitbox hitbox = hit.collider.GetComponent <Hitbox>(); if (hitbox) { if (instakill) { hitbox.Die(); } else { hitbox.Damage(damage.GetValue(), new Hitbox.Knockback()); } } retracting = !retracting; moveAmount.x = moveAmount.x - dirX * (hit.distance - skinWidth); } break; } } } transform.Translate(moveAmount); }