Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

RigibdBody Character FPS

Discussion in 'Physics' started by Nubz, Jan 24, 2015.

  1. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    First I have read a lot about the pros and cons of using characterController vs Rigidbody but still I wanted to experiment.

    I am currently trying to use the script from the Wiki
    http://wiki.unity3d.com/index.php/RigidbodyFPSWalker

    I really like how it reacts to a lot of things in the terrain etc. etc.

    Problem i am having is it climbs up any wall that has a collider on it like it is still grounded.
    Anyone have any advice on what to look into to solve this?

    Thanks in advance for your time and help in any reply.
     
  2. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    Either no one knows or the answer is someplace right under my nose o_O
     
  3. RuinsOfFeyrin

    RuinsOfFeyrin

    Joined:
    Feb 22, 2014
    Posts:
    785
    Im not sure of the solution, but the problem is the OnCollisionStay function.

    ANY collision you stay in it assumes you are grounded, this would include walls with colliders as well. The problem *i think* is when you are moving and hit a wall, and you continue to move towards the wall which you hit you are constantly firing the same collision thus OnCollisionStay is called and it thinks you are grounded.

    Not sure why it walks you up the wall, probably something to do with the collision resolution, the Force/Velocity that is applied to the controller by moving, and physics.

    While writing this, i came up with a possible solution, though obviously not tested.

    Check the normal of the colliding surface in OnCollisionStay to see if it is within a threshold for "ground"

    Hope that helps
     
  4. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    From looking in the inspector while it was doing this it became obvious that when it touches any collider like the wall of a building or the invisible ones i have so you can't walk off the terrain that it thinks it is grounded.
    Just not sure what to do to fix it or if even that script is worth a crap.

    And I am not sure what you are getting at here.
     
  5. RuinsOfFeyrin

    RuinsOfFeyrin

    Joined:
    Feb 22, 2014
    Posts:
    785
    Well, assuming your ground is flat, and on the x-z plane, the normal of the collision surface normal would be (0,1,0), anything else and you are not on the floor.

    Collision.Contact.Normal is where the normal is stored.

    You would then in OnCollisionStay check the normal of the collision, and if it is up (0,1,0) then you are grounded.
    You will also need to check OnCollisionExit for the normal of the collision you are exiting and only if it is ground (0,1,0) then set isGrounded = false;

    Hope that helps.
     
  6. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    Well that got my interest.

    Trying this from the scripting reference and from what you suggested made a lot of sense.
    Code (CSharp):
    1.  foreach (ContactPoint contact in col.contacts)
    2.         {
    3.             print(contact.thisCollider.name + " hit " + contact.otherCollider.name);
    4.             Debug.DrawRay(contact.point, contact.normal, Color.green);
    5.         }
    Telling me I am grounded only when I am on the ground and stops when I collide with something else.

    I'll post back if I get it figured out thank you.
     
  7. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    Putting this away for now it's really not worth the trouble at this point since I have a good FPS character with this script http://wiki.unity3d.com/index.php/FPSWalkerEnhanced
    as a starting point that I've added a lot of things to. I know it's an old script but it works great with what I am doing right now.

    Not giving up on a RigidBody since I do like the way the work for the most part I am just putting it aside for now.

    So thanks for the help it really did get me thinking.