- Home /
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
Answer by fafase · Mar 09, 2013 at 09:58 PM
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.
Thank you. I have a question: if 1 means flat, then if I want to check for steepness, I check if<0.7, correct?
Okay, now I've got my character checking for the slope, except when he gets close to a really steep slope, he isn't deterred from running up the mountain (I did transform.Translate(0,-5,-5) inside the if statement). The effect that's supposed to happen is sort of a sliding effect.
Could you explain again, I don't quite get what you are expecting and what is happening.
Okay, I'll explain from the start:
I have a rigidbody human controlled by $$anonymous$$ecanim using root motion. In my terrain, I have many mountains and steep slopes. $$anonymous$$y issue is that he can climb up the mountains, and I thank you for showing me how to make him check for the slope of the mountains.
The effect I want when it turns out that he is on a steep slope is that he starts sliding off. So far, the code says (I'm using Javascript, but I do know C# as well)
var Slopehit: RaycastHit;
if(Physics.Raycast(transform.position,transform.forward,Slopehit,2.0))
{
if(Vector3.Dot(Vector3.up,Slopehit.normal)<0.7)
{
transform.Translate(0,-5,-5);
}
}
But he doesn't exactly slide off, and sometimes, he even doesn't notice that there was a steep slope there.
I would think this is because you are using it somehow wrong (Thanks Captain obvious!!).
Well, first off you may want to use the rigidbody component of your character to make him fall simply using the gravity.
$$anonymous$$aybe I was wrong in the first place and you shoudl direct your ray to downwards. See at the moment you are cating forward but waht if you are facing away from the slope, the ray never hits anything. If you cast downward, you are almost assure to hit something. On top of that, you can use the cast to define if you are grounded or not. Example: You have a guy of 2 unit height
var grounded:boolean;
var canWalkForward:boolean;
var hit: RaycastHit;
if(Physics.Raycast(transform.position,-transform.up,hit,1){
grounded = true;
if(Vector3.Dot(Vector3.up,Slopehit.normal)>0.7){
canWalkForward =true;
}else{
canWalkForward = false;
}
}else{
grounded =false;
}
This way you have your variable that you can use.
Answer by systemicgames · Aug 07, 2014 at 07:39 PM
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.
Thanks @systemicgames. I haven't worked on this project in a long time, and since then I've switched to C#, but your solution seems to work as well. I wish I could accept two answers XD.
Answer by $$anonymous$$ · Sep 09, 2014 at 07:25 PM
Now Systematicgames' answer works in C#! (This is just to let anyone who is new(er) to unity know about it.) That is all.
Your answer
![](https://koobas.hobune.stream/wayback/20220613093913im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Restrict a Players Movement Up a Slope 1 Answer
Slope limit on player movement 1 Answer
how to create Magnetic repulsion ? 0 Answers
Send animation playhead to frame 0 Answers