// adjust the size of the laser depending on objects in the way void AdjustLaserDistance() { // escape if we don't have a laser child if (_laserChild == null) { return; } // check for any object that's not an ActiveAttachable RaycastHit2D hit = Physics2D.Raycast(_laserChild.transform.position, facing.ToVector2(), Mathf.Infinity, ~layerMask); if (hit.collider != null) { // calculate where the laser will stop based off the offset edge of the object we hit Vector2 endpoint = (Vector2)hit.collider.gameObject.transform.position + PhysicsUtilities.OffsetToEdgeCollider2D(hit.collider, oppositeFacing); // distance between the gun beginning of the laser and the endpoint float dist = Mathf.Sqrt(Mathf.Pow(_laserChild.transform.position.x - endpoint.x, 2) + Mathf.Pow(_laserChild.transform.position.y - endpoint.y, 2)); // set localScale of the laser - always in y because of how parent-child works on transforms in Unity _laserChild.transform.localScale = new Vector3(laserSizeX, dist, 0); } }