Slope Limitation for Rigidbody Conundrum

Hello,

On the topic of slope limitation, I’ve searched Google all over and can’t find any answers.

So my problem is that my Rigidbody character can go up any mountains and slopes. I know that the character controller has the slopeLimit parameter, but my character has to be a rigidbody due to Mecanim. I can’t find anyway to limit slope movement for him.

I’ve even tried having both a rigidbody and character controller attached to him, but that wasn’t working well (glitchy). Does anyone know anything about this? I’d really appreciate any help possible.

Hyperion

you could try to cast a ray at knee level or other position you see fit. Then you can use the hit.normal and figure out the angle with Vector3.up.

RaycastHit hit;
   if(Physics.Raycast(point.position,transform.forward,out hit,1.0f)){
      if(Vector3.Dot(Vector3.up,hit.normal)>0.7){
   }
}

See here, there is this point.position which is the point where the rayscast goes from, then it goes in the forward of the player at a distance of 1m only. You do not need to check everything in large distance.

Then you compare with the Vector3.up of the world, if the result is 1 you are on a flat ground (it won’t happen though since transform.forward is parallel and won’t hit it but for info). Now as long as the value is between 1 and 0.7 you have a slope of 0 to 45 degrees. You can modify 0.7 to nay value you see fit obviously with 0 being total perpendicular and negative value meaning you are doing some climbing.

Here is a simpler solution: Create a script called Gravity.js it doesn’t matter what name but it has to be in JavaScript. Once you are in MonoDevelop you need to delete the default code and put in.

function Start()
{
Physics.gravity = Vector3(0,-50,0);
}

You can put in any number in the middle but it has to start with - the bigger the number the stronger the gravity. You can not put a positive number such as 50 though. Because the only reason you would need to do this is because of a Rigidbody I recommend setting the Rigidbody mass to 2. Doing this allows for the player to be able to jump a normal height but the player will still not be able to walk up the slopes you created.

I really hope this helps.

Now Systematicgames’ answer works in C#!
(This is just to let anyone who is new(er) to unity know about it.)
That is all.