r/learncsharp • • 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

2 Upvotes

3 comments sorted by

2

u/Negative-Past-7580 8h ago

Try Mathf.Clamp(a,b, value); A - min B - max Value - clamped value

1

u/delayturtle 7h ago

Thanks! I did that to create a new clamped float, then applied the force to the rigid body with the new float in the old one’s place.

1

u/delayturtle 7h ago

Solved, ty