/// <summary> /// /// </summary> /// <param name="dir">1 for right, -1 for left</param> /// <returns></returns> public bool IsWaterNear(int dir) { Vector2 checkPosition = new Vector2(transform.position.x, transform.position.y) + Gravity.GetRightAt(transform.position) * dir * checkDistance; Vector2 checkRayDir = -Gravity.GetUpAt(checkPosition); RaycastHit2D hit = Physics2D.Raycast(checkPosition, checkRayDir); return(hit.collider != null && hit.collider.tag == "Water"); }
// Update is called once per frame void FixedUpdate() { //gravity m_rigidbody.AddForce(Gravity.GetGravityAt(transform.position) * gravityMultiplier, ForceMode2D.Force); //Stand up only if not totally upside down float angle = Vector3.SignedAngle(Gravity.GetUpAt(transform.position), transform.up, Vector3.forward); if (Mathf.Abs(angle) < 120) { m_rigidbody.AddTorque(Mathf.Lerp(0, standUpTorque, Mathf.Abs(angle) / 90) * Mathf.Sign(-angle)); } }
void Jump() { if (!gravityComponent.IsGrounded) { return; } Vector2 jumpDir; bool moveToTarget; Vector2 target = PointsOfInterest.ClosestPointOfInterest(m_rigidbody.position, out moveToTarget); if (moveToTarget) { bool targetIsOnRight = targetIsOnItsRight(target); if (avoidWater != null && avoidWater.enabled) { bool waterNear; if (targetIsOnRight) { waterNear = avoidWater.IsWaterNear(1); } else { waterNear = avoidWater.IsWaterNear(-1); } if (waterNear) { return; } } if (targetIsOnRight) { jumpDir = Gravity.GetUpAt(transform.position).Rotate(-_jumpAngle); } else { jumpDir = Gravity.GetUpAt(transform.position).Rotate(_jumpAngle); } } else { jumpDir = Gravity.GetUpAt(transform.position); } m_rigidbody.AddForce(jumpDir * _jumpForce, ForceMode2D.Impulse); }