I am trying to have my character walk around a cylinder. The cylinder has an outer radius around itself that acts as a gravity zone, and then a outer fall off radius that reduces gravity until the player is out of that radius, where gravity then returns to zero gravity.
I only want gravity being an influence on the outer or inner side of the cylinder, not the top or bottom. I have managed to achieve this with this code, however the gravity extends past the cylinder’s top and bottom, as if the sides of the cylinder were infinite. How can I either limit ProjectOnPlane’s length or use another method to make gravity become null once you are past the origin point or the height of the object?
[SerializeField]
float gravity = 9.81f;
[SerializeField, Min(0f)]
float innerFalloffRadius = 1f, innerRadius = 5f;
[SerializeField, Min(0f)]
float outerRadius = 10f, outerFalloffRadius = 15f;
[SerializeField, Min(0f)]
float height;
float innerFalloffFactor, outerFalloffFactor;
public override Vector3 GetGravity (Vector3 position) {
Vector3 vector = Vector3.ProjectOnPlane(transform.position - position, transform.up);
Vector3 position2 = new Vector3(transform.position.x, transform.position.y * height, transform.position.z);
float distance = vector.magnitude;
if (distance > outerFalloffRadius || distance < innerFalloffRadius ) {
return Vector3.zero;
}
float g = gravity / distance;
if (distance > outerRadius) {
g *= 1f - (distance - outerRadius) * outerFalloffFactor;
}
else if (distance < innerRadius) {
g *= 1f - (innerRadius - distance) * innerFalloffFactor;
}
return g * vector;
}