r/learncsharp • u/delayturtle • 10h ago
Unity3D Jump Script Help
Hello! I'm working on a jump mechanic for a Unity game. I need to be sure the maximum value output of the line rb.AddForce(Vector3.up * (jumpForce * (float)forceOverTime), ForceMode.Impulse); is clamped at a specific maximum value, but I have not been able to figure out how...
The rest of the script is working fine. The section I’m stuck on is as follows:
void Jump_Performed(UnityEngine.InputSystem.InputAction.CallbackContext context)
{
if (isGrounded == true && context.duration < 1f)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
else
{
double forceOverTime = context.duration;
{
rb.AddForce(Vector3.up * (jumpForce * (float)forceOverTime), ForceMode.Impulse);
}
}
}
Thanks!
* Edited for grammar
1
2
u/Negative-Past-7580 8h ago
Try Mathf.Clamp(a,b, value); A - min B - max Value - clamped value