Raycast: The closer the object is, the bigger is force.

How do i make my script make one value inversly proportional to the other value?


X gets smaller so Y gets bigger.

I’m not quite sure what you’re after, but it sounds like this (In C#)

float actualDistance = 5.0F //Replace this with the distance you got from the ray cast
float minForceDistance = 100.0F //This would be the distance where the force would be zero
float maxForce = 10.0F //This is the force that will be applied when the object is very close to the ray cast origin
float force = ( 1 - Mathf.Clamp01( actualDistance / minForceDistance ) ) * maxForce;

Hope it helps!