Search Unity

Custom CharacterController always falling

Discussion in 'Scripting' started by Mason_S, Jan 3, 2014.

  1. Mason_S

    Mason_S

    Joined:
    Jan 3, 2014
    Posts:
    1
    Hi,

    I'm making a custom character controller from scratch and I noticed a weird bug where every Update it was toggling between isGrounded and not. I stripped down the code until I found the source of the problem:

    Code (csharp):
    1. public class PlatformerController : MonoBehaviour {
    2.  
    3.     public float gravity = 40.0f;
    4.  
    5.     private Vector3 moveDirection = Vector3.zero;
    6.  
    7.     void Update() {
    8.         CharacterController controller = GetComponent<CharacterController>();
    9.  
    10.         if (controller.isGrounded) {
    11.             moveDirection = new Vector3(0, 0, 0);
    12.             Debug.Log("Grounded " + moveDirection);
    13.         } else {
    14.             moveDirection.y -= gravity * Time.deltaTime;
    15.             Debug.Log("Not grounded " + moveDirection);
    16.         }
    17.  
    18.         controller.Move(moveDirection * Time.deltaTime);
    19.     }
    20. }
    Here's what the console looks like:

    Code (csharp):
    1. Not grounded (0.0, -0.8, 0.0)
    2. Not grounded (0.0, -1.6, 0.0)
    3. Not grounded (0.0, -10.1, 0.0)
    4. Grounded (0.0, 0.0, 0.0)
    5. Not grounded (0.0, -0.7, 0.0)
    6. Grounded (0.0, 0.0, 0.0)
    7. Not grounded (0.0, -0.7, 0.0)
    8. ...
    The character in this example begins a couple units in the air then accelerates toward the ground. When it hits it becomes grounded and the moveDirection vector is set to (0.0, 0.0, 0.0). The next frame the character is no longer grounded and the moveDirection vector becomes (0.0, -0.7, 0.0).

    If I comment out the line that zeros out the moveDirection vector the character stays grounded but the vector stays at (0.0, -8.6, 0.0) and not zeroing it out causes issues with my other controls.

    My only experience with Unity3d comes from following the Physics and Scripting video tutorials and the scripting references/documentation. Am I doing this completely wrong?
     
  2. Cameron_SM

    Cameron_SM

    Joined:
    Jun 1, 2009
    Posts:
    915
    That's not a bug.

    When you zero out the move you also zero out the character's velocity for that frame, and because the previous frame the collision system projected the character out of contact with the colliders (by the tiniest fraction) it's technically not really grounded anymore. What you're effectively doing is saying "if you touch the ground, gravity suddenly stops existing!" and if there's no more downward force then of course, he's no longer really grounded, just very very very close to the ground and in limbo.

    Just apply the gravity force each frame regardless of whether or not the character is grounded. Unity actually does a full capsule sweep test projected towards the desired move position each Update step for character controllers so there's no point trying to micro-manage movement like this, just push it about and let the collision system do it's job.
     
  3. Deleted User

    Deleted User

    Guest

    the irony of gaming
     
  4. MasonGaming

    MasonGaming

    Joined:
    Oct 21, 2013
    Posts:
    14
    Ello fellow Mason^-^
    Here is my piece of advice, use the unity Controller I have tried making my own, it's no point. It's all for the same purpose right?. Just use unity's less of a hassle
     
  5. Deleted User

    Deleted User

    Guest

    he has a good point.i've tried many times.
     
  6. Deleted User

    Deleted User

    Guest

    actually now that i think of it you can. A take any object you want and put fpsmotor jv script under components or B use jv and type jump -10000000 or 10000000. what you type for #s doesn't matter although thats not the correct way to put it in script. and its not a good controller .