rigidbody walking inclines help

Hoping someone can help me here because I am struggling a bit.

The concept is I have an ant that I want to walk through a series of tunnels. In doing so I though using a rigidbody on the ant would be the best as it would follow the tunnels. Unfortunately, I am running into some issues when the tunnels get steep. When steep the ant will struggle to climb and fall over.

To remedy this I was thinking about checking the distance while the ant was moving and make sure the distance did not exceed N number ( so it couldn’t flip and it would more or less stay in contact.

First, I am not sure if there is a better approach than this so was hoping for some insight there. :slight_smile:

And second, if it is the right or at least a decent approach I wasn’t real sure on how to figure out how to get the angle to adjust the ant to.

I have attached a couple screenshots for reference.

Any insight, advice or help here would be much appreciated.

Best Regards.

235005--8419--$antwalk_2_198.png
235005--8420--$antwalk_1_340.png

I am a total unity noob but i think your problem is either the friction of the ant collider or your mass center. You could give the ant’s colliders a physic material like rubber or try to flatten the collider to lower the mass center.

i hope that helps

@basm thanks for the suggestion. I tried applying a physic material to the two colliders for the ant but it didn’t seem to have an effect.

Working with the Physics modifiers etc. is something I just don’t have a lot of familiarity with. I would think there has to be a way to do it but I just have no idea.

I am also still thinking that using a rigidbody for the ant may not be the best approach and possibly just use a Ray Cast and offset the ant rotation based off that. Of course my problem there is I am not real on the math to get the angle of the Raycast and convert that to an angle.

Hopefully someone has some additional insight because I am stuck. :-p

Thanks in advance!

As you say, you can send a raycast down from the ant to get the distance of the surface beneath and reposition the ant so it stays at a constant distance. The RaycastHit object returned by the raycast has a property called “normal” which is the normal of the surface that the ray hit. You just need to rotate the ant so that its up direction is the same as the normal (you’ll probably find Quaternion.LookRotation useful for this).

@andeeee Thanks for your help! :slight_smile:

I looked into the Quaternion.LookRotation but am not real sure on get the values in there to work properly. What I did was something like this:

var rot:Quaternion = Quaternion.LookRotation(transform.position, hit.normal);

and also tried this which seemed a bit closer but overall was the same result( except the rotation on the X was much closer ).

var rot:Quaternion = Quaternion.LookRotation( transform.position - hit.normal );

Doing either of these actually turns the ant on it’s X and Y axis. So it turns it slightly up ( via X ) but also to the right ( via Y ). When I tried to apply just the X for the normal and zero-ing out the other vectors that result seemed the same. The code for that looked like this:

var rot:Quaternion = Quaternion.LookRotation(transform.position, Vector3(hit.normal.x, 0, 0));

or this:

var rot:Quaternion = Quaternion.LookRotation( transform.position - Vector3(hit.normal.x,0,0));

I am hoping it is something fairly straight forward that I am not accounting for but really am not sure.
Any thoughts here would be extremely grateful. :slight_smile: Thanks in advance.

Thanks again for all the help and I believe I figured it out ( well at least it looks to be working properly ). :slight_smile:

Essentially here is what I did:

var rot:Quaternion = Quaternion.FromToRotation(Vector3.up, hit.normal);
transform.rotation = rot;

Again, I appreciate the help.