- Home /
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);
}
}
}
What collider do your imported models have? $$anonymous$$eshCollider, BoxCollider? Or could it be that you forgot adding any collider? :)
@nu-assets, they have different colliders, box, mesh etc, i just put an updated edit to my question which explains the problem some more
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/
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.
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.