Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

rigidbody walking inclines help

Discussion in 'Scripting' started by littlelingo, Dec 7, 2009.

  1. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    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. :)

    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.
     

    Attached Files:

  2. basm

    basm

    Joined:
    Dec 7, 2009
    Posts:
    5
    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
     
  3. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    @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!
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    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).
     
  5. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    @andeeee Thanks for your help! :)

    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:

    Code (csharp):
    1.  
    2. var rot:Quaternion = Quaternion.LookRotation(transform.position, hit.normal);
    3.  
    and also tried this which seemed a bit closer but overall was the same result( except the rotation on the X was much closer ).

    Code (csharp):
    1.  
    2. var rot:Quaternion = Quaternion.LookRotation( transform.position - hit.normal );
    3.  
    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:

    Code (csharp):
    1.  
    2. var rot:Quaternion = Quaternion.LookRotation(transform.position, Vector3(hit.normal.x, 0, 0));
    3.  
    or this:

    Code (csharp):
    1.  
    2. var rot:Quaternion = Quaternion.LookRotation( transform.position - Vector3(hit.normal.x,0,0));
    3.  
    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. :) Thanks in advance.
     
  6. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    Thanks again for all the help and I believe I figured it out ( well at least it looks to be working properly ). :)

    Essentially here is what I did:

    Code (csharp):
    1.  
    2. var rot:Quaternion = Quaternion.FromToRotation(Vector3.up, hit.normal);
    3. transform.rotation = rot;
    4.  
    Again, I appreciate the help.