Search Unity

CharacterController flickering between "Below" & "None" state.

Discussion in 'Scripting' started by canis, Nov 25, 2019.

  1. canis

    canis

    Joined:
    Oct 25, 2013
    Posts:
    79
    I try to write the movement control for myself via the "CharacterController component.
    but having the weird issue, as title mention.

    I can confirm the following information at this point.
    • The movement only apply on Update() loop.
    • The "IsGround" => CharacterController.IsGround
    • The CollisionFlags getting by calling `characterController.Move(motionVector);`
    • I found if I input Vector3.zero into Move() API above, will easier trigger the air state.
    • as video present - No Collider invoked - No Rigidbody invoked.
    • Layer Collision Matrix on project setting 'Character' & 'Ground' are able to collide each other.
    • The Handle.Label was draw on Gizmos update.
    • The red line draw when every time it fail to detect ground collider when characterController changed to IsGrounded state.


    and here is the related code to apply movement.
    Code (CSharp):
    1. Vector3 BiasGroundNormal = Vector3.up;
    2. Collider groundObject = null;
    3. CollisionFlags previousCollisionFlags;
    4.  
    5. void MoveWrapper(motionVector)
    6. {
    7.     previousCollisionFlags = characterController.Move(motionVector);
    8.     if (characterController.isGrounded)
    9.     {
    10.         Ray ray = new Ray(body.transform.position, -BiasGroundNormal); // down
    11.         if (characterController.Raycast(
    12.             ray, out RaycastHit hit,
    13.             characterController.skinWidth * 2f)) // assume double skinWidth will hit the floor.
    14.         {
    15.             groundObject = hit.collider;
    16.         }
    17.         else
    18.         {
    19.             Debug.DrawRay(ray.origin, ray.direction, Color.red, 3f);
    20.             Debug.LogError("Unable to ray ground object during : " + nameof(CollisionFlags) + "." + previousCollisionFlags, avatar);
    21.         }
    22.     }
    23. }

    I would like to know what I done wrong, if anyone known please tell me more information.
    we can do it via any other type of Raycast, but I really wanted to know why this one not working as expected.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    I think the problem is your use of the .RayCast() method in CharacterController.

    If you read these docs:

    https://docs.unity3d.com/ScriptReference/CharacterController.html

    You will see the following note for Raycast:

    "Casts a Ray that ignores all Colliders except this one."

    I presume "this one" means the Collider on your CharacterController, and hence it would ignore your ground collider(s).

    I think instead what you want is Physics.Raycast() if you are trying to sense the ground.

    You may need to either use layers and specify that the raycast ignores your character, or else each time you raycast, turn off your character's collider, then switch it back on after.
     
    canis likes this.
  3. canis

    canis

    Joined:
    Oct 25, 2013
    Posts:
    79
    Wow, it was ! thanks for pointing that out.!!
     
    Kurt-Dekker likes this.