Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 RandomUser123 · Sep 06, 2015 at 05:55 PM · rigidbodyangleslope

Unity, walking up small steps with rigidbodies

I am trying to make my character move up small steps in my game, I have seen people using invisible sloped colliders to simulate a ramp but this is not effective in my game as there are many small segments in which my character can walk over, such as a small fence, or pieces on a bent roped bridge.

When using the character controller, there is a slope limit that allows the character to walk over small objects, I would like to achieve a similar effect with rigidbodies. I am using the following code but in my test picture below, there is no slope on the objects so it never returns true.

EDIT

So i've managed a workaround which uses 2 raycasts, one at the base of my characters feet and one just above it, I only run the code if the one at the feet touches an object, this works for standard cubes and spheres from unity, but it will not work on my imported models, any ideas why?

EDIT 2

After playing around with it some more this morning, it seems that the code below works but only if the object in question (3 cubes below) are passing through the terrain slightly, that is, if they are colliding with another object, so if i get all my objects and lower them slightly into the ground, my code works, but not if they are sitting on the terrain. I am very stumped at this and would appreciate any ideas.

 void CheckForSteps()
 {
     RaycastHit hit;
     RaycastHit headHit;

     Debug.DrawRay(transform.position, transform.forward, Color.red, 1.0f);
     Debug.DrawRay(transform.position + new Vector3(0,0.4f, 0), transform.forward, Color.red, 1.0f);

     if (Physics.Raycast(transform.position, transform.forward, out hit, 1.0f) && !Physics.Raycast(transform.position + new Vector3(0,0.4f, 0), transform.forward, out headHit, 1.0f))
     {
         Debug.Log("Hitting");
         if (Vector3.Dot(Vector3.up, hit.normal) < 0.7f)
         {
             caecusRigidbody.GetComponent<Rigidbody>().AddForce(transform.up, ForceMode.VelocityChange);
         }
     }
 }

alt text

capture.png (320.0 kB)
Comment
Add comment · Show 2
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 nu-assets · Sep 07, 2015 at 05:36 AM 0
Share

What collider do your imported models have? $$anonymous$$eshCollider, BoxCollider? Or could it be that you forgot adding any collider? :)

avatar image RandomUser123 · Sep 07, 2015 at 09:57 AM 0
Share

@nu-assets, they have different colliders, box, mesh etc, i just put an updated edit to my question which explains the problem some more

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by IronKnigh · Oct 26, 2018 at 11:01 PM

M8, here you go. This guy does a much better job of explaining it than I do. Best of luck! https://cobertos.com/how-to-climb-stairs-unit3d/

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
avatar image
1

Answer by Eudaimonium · Sep 06, 2015 at 08:41 PM

What you're doing is doing a raycast from character's position downwards.

Character's position is likely somewhere in it's middle, and it's never going to hit the obstacle because you're likely to hit it with the edges of your colliders, before your "position" (player's middle) is over it.

What you can do instead is implement an OnCollisionEnter function within the script of your character: http://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html

Using the function's argument, you can find all contact points your character currently has with anything else. Each contact point also contains a normal. Using this you can find out if you're just standing on ground (one contact point, normal equals or is close to Vector3.Up), or touching something else as well (multiple points, normal not as close to Vector3.up).

Next step would be seeing what exactly you're touching, and determining it's dimensions. If it's highest point is below some defined value (stepHeight or such), then you need to lift the player up by step height or so.

Logic is a bit tricky really, and I see no reason to go through all this. If you intent for these objects to not be actual obstacles for the player, why add a collider to them at all? Just have the player pass through them. If they're decorations for terrain, passing through them would be expected.

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 RandomUser123 · Sep 06, 2015 at 10:57 PM 0
Share

I tried using the Collision points before but it had a negative effect on some of my other movement scripts, I also think raycasts are more efficient for what I need it for. I use colliders because the example above is a very primitive one, I actually have a big game world which a lot of small steps and ledges that my character cannot run over requiring him to jump which is inconvenient.

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

30 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 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 avatar image avatar image avatar image avatar image

Related Questions

Checking slope angle the object is standing on? 1 Answer

How to change an angle of object with Rigidbody2D in a specific range? 0 Answers

Problem with getting constructional slope angle. 0 Answers

Get angle from impact 2 Answers

How to set specific angle between 2 game objects in unity 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