Search Unity

Character Collision flags rapidly flashes

Discussion in 'Scripting' started by Radishking27, Jun 26, 2019.

  1. Radishking27

    Radishking27

    Joined:
    Jun 19, 2019
    Posts:
    2
    I'm having trouble with this piece of code. Specifically I'm trying to code this collision system below the character controller that can tell me if I'm grounded or not. The only problem is that the isGrounded bool rapidly switches.

    The Character Controller code also includes a jump and gravity system as well as a progressive movement system (Slowly builds up speed) .

    Here's the essential code. I hope you guys can find the solution to the problem.
    Code (CSharp):
    1. void GroundedCheck()
    2.     {
    3.         if (characterController.collisionFlags == CollisionFlags.Below || (characterController.collisionFlags & CollisionFlags.Below) != 0)
    4.         {
    5.  
    6.             isGrounded = true;
    7.         }
    8.         else
    9.         {
    10.             isGrounded = false;
    11.         }
    12.     }
    13.  
    14.     void Jumping()
    15.     {
    16.         if (isGrounded == true)
    17.         {
    18.             JumpSpeed = 0;
    19.  
    20.             if (Input.GetButtonDown("Jump"))
    21.             {
    22.                 JumpTimer = 0f;
    23.                 JumpSpeed = JumpIntensity;
    24.             }
    25.         }
    26.         if (isGrounded == false)
    27.         {
    28.             if (JumpTimer == JumpInterval){                                     //Check if JumpTimer is up
    29.                 JumpSpeed -= GravitySpeed;                                      //Lower Jump Speed
    30.                 if (JumpSpeed <= -JumpGravity)                                  //Check if jump speed is faster than gravity
    31.                 {
    32.                     JumpSpeed = -JumpGravity;                                   //Set Speed to max gravity
    33.                 }
    34.             }
    35.             else if (JumpTimer < JumpInterval && Input.GetButton("Jump"))       //Check if JumpTimer is under Max and check if Jump button is held down    
    36.             {
    37.                 JumpTimer += Time.deltaTime;                                    //Add time to Timer
    38.                 JumpSpeed = JumpIntensity;                                      //Keep Jump going
    39.             }
    40.             else                                                                //if the button is not held or the Jump timer is over interval
    41.             {
    42.                 JumpTimer = JumpInterval;                                       //Set JumpTimer at end
    43.             }
    44.         }
    45.     }
    Also, Quick Question: Is it practical to use Char collision flags or is there another accurate way to check collisions? Thanks in advance!
     
  2. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    425
    I have never used those collision flags or the characterController component, but that doesn't mean they're not good. There are many ways in wich you can check for the ground. Usually they involve trhowing a small ray downwards and see if it hits something, it won't give those false positives you're having. Regarding your current script, an option may be to just check if the collision bellow returns true for a couple of frames, say 12 in a row (half a second) before deciding that indeed he's not grounded.

    I'm not sure if the component you're using is rather old or what, but I haven't seen it before. You may want to just get one example from the standard assets, if you're aiming for 3D the "Third person controller" may be a good start. It works right out of the box, and you can study it's code and modify it to learn about it. Doing your first controller is a bit difficult, you could use some tutorial like the ones on youtube, or better one the Unity ones. The advantage is that they already give you the models and the animations and they guid you through the process.
     
  3. Radishking27

    Radishking27

    Joined:
    Jun 19, 2019
    Posts:
    2
    I forgot to say that when the player's on like a slope I want to make sure it stays on the floor. Would i still use rays?
     
  4. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    425
    yeah, a raycast to the ground will do for slopes, just make sure you cast it from the center and that it has some length to not get a positive when the character just bounce slightly. You can use Debug.DrawRay to see it in the editor screen so that you can know if it's well placed. Also, you may want to not throw one ray every frame, this way it consume less resources. Remember that there are many frames in a second, and you don't need to check that match.
    You can also just stick with what you have and just recheck a couple of frames before concluding it's not in the ground. Also you can try to see it frame by frame and try to figure why it's bouncing.