public override bool HandleFixedUpdate() { //TODO; this is such a hack if (InputManager.Instance.IgnoreInput) { return(true); } if (!f_controller.Grounded) { return(true); } if (f_controller.Backwards) { f_controller.Backwards = false; } if (Mathf.Abs(f_controller.Velocity.x) < 0.1f) { f_controller.GroundMovement.MovingRight = !f_controller.GroundMovement.MovingRight; } f_controller.Velocity = Vector2.zero; GroundTouch gt = f_controller.GroundMovement.Move((f_controller.GroundMovement.MovingRight ? 1 : -1) * SPEED * BossFightRhino.SPEED); if (m_stopAtEdges) { int airDirection = GroundMovementRaycast.AirDirection(gt); if (airDirection < 0) { f_controller.GroundMovement.MovingRight = true; } else if (airDirection > 0) { f_controller.GroundMovement.MovingRight = false; } } return(true); }
public static int AirDirection(GroundTouch gt) { int ret = 0; if ((gt & GroundTouch.Left) == 0) { ret -= 1; } if ((gt & GroundTouch.HalfLeft) == 0) { ret -= 2; } if ((gt & GroundTouch.HalfRight) == 0) { ret += 2; } if ((gt & GroundTouch.Right) == 0) { ret += 1; } return(ret); }
public GroundTouch Move(float speed) { // Do 5 downward raycasts // Downward = downward in regards to character { GroundTouch ret = 0; Vector2 transformedDownward = f_controller.transform.TransformVector(Vector2.down); float RAY_LENGTH = f_halfHeight * f_controller.transform.localScale.y + EXTRA_RAY_LENGTH; RaycastHit2D hitL = Physics2D.Raycast(f_controller.transform.TransformPoint(new Vector3(-f_halfWidth - OUTER_EXTEND, f_halfHeight, 0)), transformedDownward, RAY_LENGTH, f_groundMask); RaycastHit2D hitHL = Physics2D.Raycast(f_controller.transform.TransformPoint(new Vector3(-f_halfWidth, f_halfHeight, 0)), transformedDownward, RAY_LENGTH, f_groundMask); RaycastHit2D hitC = Physics2D.Raycast(f_controller.transform.TransformPoint(new Vector3(0, f_halfHeight, 0)), transformedDownward, RAY_LENGTH, f_groundMask); RaycastHit2D hitHR = Physics2D.Raycast(f_controller.transform.TransformPoint(new Vector3(f_halfWidth, f_halfHeight, 0)), transformedDownward, RAY_LENGTH, f_groundMask); RaycastHit2D hitR = Physics2D.Raycast(f_controller.transform.TransformPoint(new Vector3(f_halfWidth + OUTER_EXTEND, f_halfHeight, 0)), transformedDownward, RAY_LENGTH, f_groundMask); if (hitL) { ret |= GroundTouch.Left; } if (hitHL) { ret |= GroundTouch.HalfLeft; } if (hitC) { ret |= GroundTouch.Centre; } if (hitHR) { ret |= GroundTouch.HalfRight; } if (hitR) { ret |= GroundTouch.Right; } if (speed > 0) { MovingRight = true; if ( (!hitC && hitR && hitHR && Vector2.Angle(Vector2.right, hitR.point - hitHR.point) > MAX_ABS_SLOPE) || (hitC && hitHR && Vector2.Angle(Vector2.right, hitHR.point - hitC.point) > MAX_ABS_SLOPE) ) { // if (hitR && hitHR && Vector2.Angle(Vector2.right, hitR.point - hitHR.point) > MAX_ABS_SLOPE) { //Debug.Log("Abort Right: " + Vector2.Angle(Vector2.right, hitR.point - hitHR.point), f_controller); return(ret); } } else { MovingRight = false; if ( (!hitC && hitL && hitHL && Vector2.Angle(Vector2.left, hitL.point - hitHL.point) > MAX_ABS_SLOPE) || (hitC && hitHL && Vector2.Angle(Vector2.left, hitHL.point - hitC.point) > MAX_ABS_SLOPE) ) { // if (hitL && hitHL && Vector2.Angle(Vector2.left, hitL.point - hitHL.point) > MAX_ABS_SLOPE) { //Debug.Log("Abort Left: " + Vector2.Angle(Vector2.left, hitL.point - hitHL.point), f_controller); return(ret); } } Vector2 middleVector = (hitHR.point - hitHL.point).normalized; if (hitHL && hitHR) { // corrrecting the current transform RaycastHit2D hitQL = Physics2D.Raycast(f_controller.transform.TransformPoint(new Vector3(-f_halfWidth / 2f, f_halfHeight, 0)), Vector2.down, RAY_LENGTH, f_groundMask); RaycastHit2D hitQR = Physics2D.Raycast(f_controller.transform.TransformPoint(new Vector3(f_halfWidth / 2f, f_halfHeight, 0)), Vector2.down, RAY_LENGTH, f_groundMask); if (!hitQL) { hitQL = hitHL; } if (!hitQR) { hitQR = hitHR; } middleVector = (hitQR.point - hitQL.point).normalized; f_controller.transform.rotation = Quaternion.FromToRotation(Vector2.up, Vector3.Cross(middleVector, Vector3.back)); if (hitC && (hitL || hitR)) { f_controller.transform.position = hitC.point; } else { //f_controller.transform.position = (hitHR.point + hitHL.point) / 2f; f_controller.transform.position = (hitQR.point + hitQL.point) / 2f; } } else if (hitC) { f_controller.transform.position = hitC.point; if (!hitHL && !hitHR) { middleVector = Vector2.right; f_controller.transform.rotation = Quaternion.identity; } else if (hitHL) { middleVector = (hitC.point - hitHL.point).normalized; f_controller.transform.rotation = Quaternion.FromToRotation(Vector2.up, Vector3.Cross(middleVector, Vector3.back)); } else // hitHR { middleVector = (hitHR.point - hitC.point).normalized; f_controller.transform.rotation = Quaternion.FromToRotation(Vector2.up, Vector3.Cross(middleVector, Vector3.back)); } } else { if (hitHL) { middleVector = Vector2.right; f_controller.transform.rotation = Quaternion.identity; //f_controller.transform.position = hitHL.point + Vector2.right * f_halfWidth; } else if (hitHR) { middleVector = Vector2.right; f_controller.transform.rotation = Quaternion.identity; //f_controller.transform.position = hitHR.point + Vector2.left * f_halfWidth; } } //TODO: What do I do if not both inner ones hit (pointy edge?) // preparing the velocity change for the transform // this is a bit complicated due too many different cases if (speed > 0) { if (hitHR) { f_controller.Velocity = middleVector * speed; } else if (hitC) { f_controller.Velocity = (hitC.point - hitHL.point).normalized * speed; } else { f_controller.Velocity = (hitHL.point - hitL.point).normalized * speed; } } else { if (hitHL) { f_controller.Velocity = -middleVector * -speed; } else if (hitC) { f_controller.Velocity = (hitC.point - hitHR.point).normalized * -speed; } else { f_controller.Velocity = (hitHR.point - hitR.point).normalized * -speed; } } //TODO; clean up the above; it doesn't seem right // this means only one side touched if (ret == GroundTouch.Left || ret == GroundTouch.HalfLeft || ret == GroundTouch.Centre || ret == GroundTouch.HalfRight || ret == GroundTouch.Right) { f_controller.Velocity = f_controller.transform.right * speed; } if (!(hitL && hitR && hitHL && hitHR)) { return(ret); } Vector2 intersection = MathExtension.Inters(hitL.point, hitHL.point, hitR.point, hitHR.point); Debug.DrawLine(intersection, intersection + Vector2.up * 0.1f, Color.red); return(ret); } }
void Awake() { GT = this; }
public override bool HandleFixedUpdate() { //TODO; this is such a hack if (InputManager.Instance.IgnoreInput) { return(true); } if (!f_controller.Grounded) { return(true); } if (m_cooldown == -1) { if (f_controller.Backwards) { f_controller.Backwards = false; // if (f_controller.Velocity.x != 0) { // m_walkingRight = // } } if (Mathf.Abs(f_controller.Velocity.x) < 0.1f) { Cancel(); } //TODO; does not care about transform if (f_controller.Velocity.x < -0.1f) { f_controller.GroundMovement.MovingRight = false; } else if (f_controller.Velocity.x > 0.1f) { f_controller.GroundMovement.MovingRight = true; } f_controller.Velocity = Vector2.zero; GroundTouch gt = f_controller.GroundMovement.Move((f_controller.GroundMovement.MovingRight ? 1 : -1) * CHARGE_SPEED * BossFightRhino.SPEED); if (m_stopAtEdges) { int airDirection = GroundMovementRaycast.AirDirection(gt); if (airDirection != 0) { Cancel(); } } } else { if (m_cooldown == CHARGE_COOLTIME) { f_futureStates = m_futureStatesHelper; m_cooldown = -1; } ++m_cooldown; } return(true); }