# slippery slopes

Discussion in 'Scripting' started by dacloo, Sep 1, 2007.

1. ### dacloo

Joined:
Jun 30, 2005
Posts:
469
Now "Slippery Slopes" may sound like a game, but in fact its an issue I'm having currently in a platform game.

The current character controller checks the steepness, but it doesn't make the character slide down the slope until it reaches a not-so-steep part.

The problem is that I need to move the character in opposite direction of the "steepness". How do I determin, using the character controller, which direction the player should fall to?

thanks

Joined:
Mar 16, 2005
Posts:
3,966
3. ### dacloo

Joined:
Jun 30, 2005
Posts:
469
Hi!

I am currently using it to determin the 'steepness'
but I am not sure if normal.y is in fact the correct steepness.

But the real question: Now I have the normal, how can
I determin the direction for the character to fall to
and not make it 'stick' to the mesh?

File size:
35.8 KB
Views:
2,227

Joined:
Mar 16, 2005
Posts:
3,966
5. ### dacloo

Joined:
Jun 30, 2005
Posts:
469
That worked! The implementation was hard, because it can work against you playability wise. Thanks!

### Unity Technologies

Joined:
Mar 16, 2005
Posts:
3,966
Actually i am very interested in the code you wrote to make it work in practice.
When I tried it one of the game play problems is actually how to differentiate small bumps and large slopes. Because based on the slope you can run over a tiny bump and you get pushed back?

Did you find a good way to solve that?

7. ### dacloo

Joined:
Jun 30, 2005
Posts:
469
Hi Joachim,

Exactly! That was the problem.
I solved it (partially!) by looking at the moveDirection.y.
If you are jumping it is y>0. Then I simply ignore the 'bounce back'.

The code got real complicated in logic (lots of if/thens).
I also use a coroutine that does this when bumping:

Code (csharp):
1. maywalk = false;
2. yield new WaitForSeconds(0.5);
3. maywalk = true;
I can send you the whole (messy) code but it is really specific to this particular
game (not a generic solution).

8. ### Neural Echo

Joined:
Jul 5, 2007
Posts:
83
Is it possible to measure the length of the vertex you are colliding with and only engage the slide motion if the vertex is greater than a certain length?

9. ### Brian-Kehrer

Joined:
Nov 7, 2006
Posts:
409
we solved this by calculating the actual slope angle, and slide direction, and figuring out the component gravity.
It should look roughly like this I think
Code (csharp):
1.
2. if(groundSlopeAngle > 45){
4. }
5. else{
6. slideVal = slideVal*.95f;
7. }
8. motionDirection = inputMotion;
9.
10. motionDirection += slideVal;
11.

Over a small 'bump' if you run over it, your running speed will overwhelm the effects of sliding. However if you try to run up a hill, you will only make it so far before the cumulative effect of sliding overpowers it.

Add some conditions to determine when you are sliding, and how it falls off.

we solved this by calculating the actual slope angle, and slide direction, and figuring out the component gravity.
It should look roughly like this I think
Code (csharp):
1.
2. if(groundSlopeAngle > 45){
4. }
5. else{
6. slideVal = slideVal*.95f;
7. }
8. motionDirection = inputMotion;
9.
10. motionDirection += slideVal;
11.

Over a small 'bump' if you run over it, your running speed will overwhelm the effects of sliding. However if you try to run up a hill, you will only make it so far before the cumulative effect of sliding overpowers it.

Add some conditions to determine when you are sliding, and how it falls off.

Here is how we got the angles needed
Code (csharp):
1.
2.
3.         Vector3 temp = Vector3.Cross(hit.normal, Vector3.down);
4.         groundSlopeDir = Vector3.Cross(temp, hit.normal);
5.         groundSlopeAngle =  Vector3.Angle(hit.normal,Vector3.up);
6.     }
7.

10. ### Fishypants

Joined:
Jan 25, 2009
Posts:
444
Hey Brian, sorry for posting in an old post, but I am trying to get your example working, but I don't think I'm implementing it correctly . . .

So im trying to get the function, "OnControllerColliderHit" working, but when I use the code, it gives me these errors:

';' expected. Insert a semicolon at the end.
BCE0043: Unexpected token: )
BCE0044: expecting :, found 'temp'.

I'm in the process of learning unity, so certain things aren't as obvious to me yet as they should be, but could you or someone else explain to me the proper way to use this code?

Should I add your code to a script and attach it to the object and it should just go? I'm a little confused.

11. ### Fishypants

Joined:
Jan 25, 2009
Posts:
444
Wait, nevermind. I figured it out Thanks for posting the code! I had to tweak it a bit to work with my scene but all is well now. It works great!

12. ### Brian-Kehrer

Joined:
Nov 7, 2006
Posts:
409
No problem. Look for that bit of code appearing in our upcoming iPhone game .

13. ### javiermorawski

Joined:
Feb 16, 2012
Posts:
2
Hey Fishypants, I know that this post is WAAAAAAYY old by now, but I'm also trying to implement this, and I am getting the same error messages.
Like you (at the time of your post) am relatively new to Unity javascript, and I have been cracking my brain on this.
You wouldn't happen to remember what your fix was do you?
Error: ';' expected. Insert a semicolon at the end.

and its talking about the line:

Vector3 temp = Vector3.Cross(hit.normal, Vector3.down);

of code:

Code (csharp):
1.     {
2.     Vector3 temp = Vector3.Cross(hit.normal, Vector3.down);
3.     groundSlopeDir = Vector3.Cross(temp, hit.normal);
4.     groundSlopeAngle =  Vector3.Angle(hit.normal,Vector3.up);
5.
6.     }

14. ### javiermorawski

Joined:
Feb 16, 2012
Posts:
2
Nevermind... Also figured it out

15. ### RonHiler

Joined:
Nov 3, 2011
Posts:
212
Very cool! I thought I was the only one on the planet that preferred their braces have the same indentation as the code block they go with. Thank you for letting me know I'm not alone!