Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Hyperion · Mar 06, 2013 at 06:22 AM · rigidbodymecanimlimitslope

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hyperion · Mar 10, 2013 at 02:02 AM 0
Share

Thank you. I have a question: if 1 means flat, then if I want to check for steepness, I check if<0.7, correct?

avatar image Hyperion · Mar 10, 2013 at 02:16 AM 0
Share

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.

avatar image fafase · Mar 10, 2013 at 08:53 PM 0
Share

Could you explain again, I don't quite get what you are expecting and what is happening.

avatar image Hyperion · Mar 10, 2013 at 11:12 PM 0
Share

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.

avatar image fafase · Mar 11, 2013 at 07:42 AM 0
Share

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.

Show more comments
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hyperion · Aug 07, 2014 at 09:41 PM 0
Share

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.

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges