float?wallTest(Vector2 p0, Vector2 p1, float forceX, out bool pushed, Func <Vector2, bool> normalCheck) { pushed = false; var result = DoubleLineCast.Cast(p0, p1, MooseController.CollisionLayerMask); if (!result.HasValue) { return(null); } if (normalCheck(result.Value.normal)) { return(null); } if (_mooseController.Charging) { var smashable = result.Value.collider.GetComponent <Smashable>(); if (smashable) { smashable.Smash(); return(null); } } var targy = result.Value.collider.GetComponent <Rigidbody2D>(); if (targy != null && !targy.isKinematic && result.Value.normal.x * forceX < 0) { pushed = true; targy.AddForceAtPosition(20 * forceX * Vector2.right, result.Value.point); } return(result.Value.depth * -Mathf.Sign(result.Value.normal.x)); }
Vector2 wallCollision(Vector2 newPos) { float firstY = Mathf.Max( _vel.y < 0 ? -_vel.y : 0, _heroDim.InsetY ); float lastY = Mathf.Min( _vel.y > 0 ? _heroDim.Height - _vel.y : _heroDim.Height, _heroDim.Height - _heroDim.InsetY ); float stepY = (lastY - firstY) / 2; for (float offy = firstY; offy < lastY + 1e-9f; offy += stepY) { var offset = Vector2.up * offy; var pt0 = newPos + offset - _heroDim.HalfWidth * Vector2.right; var pt1 = newPos + offset + _heroDim.HalfWidth * Vector2.right; Debug.DrawLine(pt0, pt1, Color.green); // hmm var maybeHit = DoubleLineCast.Cast(pt0, pt1, CollisionLayerMask); if (maybeHit.HasValue && normalIsWall(maybeHit.Value.normal)) { newPos.x = maybeHit.Value.point.x + Mathf.Sign(maybeHit.Value.normal.x) * _heroDim.HalfWidth; if (maybeHit.Value.normal.x * _vel.x < 0) { _vel.x = 0; } } } return(newPos); }
Vector2?vertTestAtOffset(Vector2 newPos, float offsetScale) { var offVec = offsetScale * new Vector2(_heroDim.HalfWidth - _heroDim.InsetX, 0); var pt0 = newPos + Vector2.up * _heroDim.Height + offVec; var pt1 = newPos + offVec; var maybeHit = DoubleLineCast.Cast(pt0, pt1, CollisionLayerMask); Debug.DrawLine(pt0, pt1, Color.green); if (maybeHit.HasValue && !maybeHit.Value.embedded) { return(reactToVertCollision(newPos, maybeHit.Value, Vector2.zero)); } return(null); }
Vector2 reactToVertCollision(Vector2 newPos, DoubleLineCast.Result hit, Vector2 offset) { if (normalIsGround(hit.normal)) { _vel.y = 0; newPos.y = hit.point.y; if (!GravitySetting.Reverse) { if (_cantSnapCounter == 0) { startSnap(hit.collider, hit.rigidbody, newPos - offset, hit.normal); } } } else if (normalIsRoof(hit.normal)) { if (_vel.y > 0) { _vel.y = 0; } newPos.y = (hit.point - Vector2.up * _heroDim.Height).y; if (GravitySetting.Reverse) { if (_cantSnapCounter == 0) { newPos.y = hit.point.y; startSnap(hit.collider, hit.rigidbody, newPos - offset, hit.normal); newPos.y = (hit.point - Vector2.up * _heroDim.Height).y; } } } return newPos; }