- Home /
Prevent Rigidbody from Climbing Steep Slopes using Forces (C#)
Hello all!
I have been wracking my brain for weeks trying to solve this issue. Any assistance is greatly appreciated!
I’ve created a custom Rigidbody FPS controller. I need to prevent it from climbing steep slopes. Currently, when it steps onto a steep slope, it slides downwards, but since it can repeatedly step on and slide down such a slope in rapid succession, it causes a perceptible “jitter”. This is what I’m trying to prevent. I wish to use forces at the base of the slope to prevent the character from making that step onto it.
With sufficient velocity, the Rigidbody should still be able to slide partway up, but after it has slid down again, it should remain at its base unless it runs again.
I can’t utilize friction, because the Rigidbody uses a frictionless material to prevent other issues.
Disabling forward input altogether doesn’t work, because the character should still slide along the base of the slope if the input isn’t exactly perpendicular to its base.
Finally, force application must be precise, as the character itself be standing on a Rigidbody, which must remain unaffected.
The following code is very close, but the jitter returns when there is a high approach angle:
private void SlopePreventClimb()
{
if ( !isGrounded || isSliding) return;
var hit = new RaycastHit();
var movementDir = Body.velocity.normalized;
if ( !Physics.Raycast( transform.position + ( movementDir * 0.3f ), Vector3.down, out hit ) ) return;
if (Vector3.Angle(Vector3.up, hit.normal) < 45f) return;
var force = transform.InverseTransformVector( hit.normal / Time.fixedDeltaTime * Body.velocity.magnitude);
force.y *= -1f;
Body.AddForce(force, ForceMode.Acceleration);
}
Again, I’m quite stumped. Any help would be greatly appreciated! Thank you in advance. :)
Your answer
Follow this Question
Related Questions
Object move on z axis while giving force on x axis 0 Answers
How to hit gameobjects to go into the opposite direction of hit? 1 Answer
Distribute terrain in zones 3 Answers
object move towards to mouse position 0 Answers
Set Velocity at relative position. 2 Answers