void ApplyCentripetalForce() { Vector2 offset = (Vector2)transform.position - center.GetGlobalCenter(); //the vector from the center of the planet to the player //Quaternion rotationOfOffset = Quaternion.FromToRotation(Vector3.up, offset); //rotation of the offset vector from vertical //Vector2 tangent = rotationOfOffset * Vector3.right; //the vector perpendicular to the offset vector in the xy plane //Vector2 projVelocity = Vector3.Project(body.velocity, tangent); //the player's tangential velocity Vector2 projVelocity = Radium.TangentVelocity(transform.position, body, center.GetGlobalCenter()); //print("Player velocity vector:" + projVelocity + " speed:" + projVelocity.magnitude); if (projVelocity.magnitude > Mathf.Epsilon) { Vector2 force = -offset.normalized * Time.fixedDeltaTime * projVelocity.sqrMagnitude * body.mass / offset.magnitude; body.AddForce(force, ForceMode2D.Impulse); } }
//takes the force that the player would like to exert (for movement) and limits it so the player doesn't go too fast Vector2 SpeedLimitedForce(Vector2 rawForce) { Vector2 projVelocity = Radium.TangentVelocity(transform.position, body, center.GetGlobalCenter()); float dot = Vector2.Dot(rawForce, projVelocity); if (dot < 0) { return(rawForce); //if your applied force is against the current velocity, you get full force } if (projVelocity.magnitude >= maxTangentialSpeed) { return(Vector2.zero); //if you are already going full speed, you get no force } //otherwise, you get enough force to get you up to full speed or full force, whichever is less float maxAllowedForce = Mathf.Min(maxTangentialSpeed - projVelocity.magnitude, rawForce.magnitude); return(rawForce.normalized * maxAllowedForce); }