Ground clamping a rigidbody on slope

I decided to press out on my own and make my own character controller using a rigidbody . It’s working well overall, I’ve got it moving around predictably etc. I previously used the Character Controller but I’m not happy with having to use the capsule collider. The point I’m at now I’m trying to get some kind of ground clamp working so when the character moves down a hill it sticks to the hill rather than bouncing down. I’ve been looking around for information on this problem, but I haven’t had much luck. I thought I had it by applying a negative velocity on the y axis. Initially that worked well, but then it kind of forces the character to slide down any hill that’s too steep, as well as causing collision issues with the hill itself. Then I thought using a Raycast Hit and finding the hit point then using rb.MovePosition to move the player to the point at which the raycast hits the collider.

Here’s my code.

RaycastHit hit;

Physics.Raycast(transform.position, Vector3.down, out hit);

Vector3 groundBelow = new Vector3(0, hit.point.y, 0);

rb.MovePosition(rb.position - groundBelow);

I thought I had it but it basically just tries everything it can to drill into the ground, and overall acts like garbage.

Any help would be really appreciated.
I’m doing this in C# but I can translate over from JavaScript no problem.

Thanks.

Eh, well I figured it out. It’s pretty simple now that I see it. I was trying for a complicated solution.

In the movement control Vector3 I applied a negative number to the y axis and that did it.

float groundclamp = -.75;

Vector3 moveDir = new Vector3(Input.GetAxisRaw(“Horizontal”), groundclamp, Input.GetAxisRaw(“Vertical”));

SO that was stupidly easy. I used the code from the FPS walker So from here I can extrapolate where the player is moving down a ramp and apply the clamp when needed, and all that good stuff.

http://wiki.unity3d.com/index.php?title=FPSWalkerEnhanced#C.23_Version

Thanks Eric Haines, who made the FPS walker. That helped a lot!