Search Unity

Character controller walking over colliders

Discussion in 'Physics' started by Person96, Nov 24, 2018.

  1. Person96

    Person96

    Joined:
    Apr 16, 2018
    Posts:
    2
    Alright, so I have this bug as to where the player character is able to walk over certain colliders, mainly NPC colliders.



    Certain colliders, such as the environment and some NPC's don't have this problem as seen in this GIF.



    Why is that and how can I solve it?

    Keep in mind that the NPC that I can walk over has a collider listed on another Layer titled Hitbox while the other NPC does not, could this be a problem? Also the environment I've listed as static so that solved that problem. The player character also has two colliders on him, one listed on the Hitbox layer as well in conjunction with his other regular collider, I'm not sure if having two colliders is necessary but I did that anyway just to be safe. I also have yet to create a Navmesh for the environment so that's not a problem at the moment.

    This is my entire movement code for the player character. I'm also using a character controller for the player and a rigidbody for the NPC's.

    Code (CSharp):
    1. public class Movement : MonoBehaviour {
    2.  
    3.     private float w_speed = 8f;    //regular speed
    4.     private float c_speed = 1.5f; //crouch speed
    5.     private float no_speed = -1f; //stops running in walls
    6.     private float standardHeight = 2f; //non crouch
    7.     private float crouchingHeight = 0.3f; //crouch
    8.     private float crouchDistance = 1.4f; //to not stand
    9.     private float talkRadius = 2.5f;
    10.     private float radiusMove = 0.6f; //to not run in walls
    11.     private float radiusMoveLong = 1f; //to not run in walls diagonal
    12.     private float jumpForce = 16f; //to jump
    13.     private float gravity = 50f; //pulls down
    14.  
    15.     private Vector3 moveDir = Vector3.zero;
    16.     private Vector3 moveRot;
    17.  
    18.     public bool crouching = false;
    19.     public bool canJumpForward;
    20.     public bool canMove;
    21.     public bool isTalking;
    22.     private bool playerMoving;
    23.     private Vector2 lastMove;
    24.  
    25.     private CharacterController controller;
    26.     public AudioSource jumpAudio;
    27.     public Transform target;
    28.     public GameObject sprite;
    29.     public Collider[] attackHitBoxes;
    30.     Animator spriteAnimation;
    31.  
    32.     void Start () {
    33.         controller = gameObject.GetComponent<CharacterController>();
    34.         spriteAnimation = sprite.GetComponent<Animator>();
    35.         canMove = true;
    36.         canJumpForward = true;
    37.     }
    38.  
    39.     void Update()
    40.     {
    41.         playerMoving = false;
    42.  
    43.         var horizontalAxis = Input.GetAxis("Horizontal");
    44.         var verticalAxis = Input.GetAxis("Vertical");
    45.         var up45 = (transform.forward + transform.up * 1.3f).normalized;
    46.         var down45 = (transform.forward - transform.up * 1.3f).normalized;
    47.  
    48.         RaycastHit hitTop; //for crouching
    49.         RaycastHit hitForward; //for stopping
    50.         RaycastHit hitTarget; //for facing other conversationalist
    51.  
    52.         Ray playerFront = new Ray(transform.position, transform.forward);
    53.         Ray playerTop = new Ray(transform.position, Vector3.up);
    54.         Debug.DrawRay(transform.position, Vector3.up * crouchDistance);
    55.  
    56.         Ray playerMoveDiagonalRight = new Ray(transform.position, transform.TransformDirection(1, 0, 1));
    57.         Debug.DrawRay(transform.position, transform.TransformDirection(1, 0, 1) * radiusMove);
    58.  
    59.         Ray playerMoveDiagonalLeft = new Ray(transform.position, transform.TransformDirection(-1, 0, 1));
    60.         Debug.DrawRay(transform.position, transform.TransformDirection(-1, 0, 1) * radiusMove);
    61.  
    62.         Ray playerMoveDiagonalUp = new Ray(transform.position, up45);
    63.         Debug.DrawRay(transform.position, up45 * radiusMoveLong);
    64.  
    65.         Ray playerMoveDiagonalDown = new Ray(transform.position, down45);
    66.         Debug.DrawRay(transform.position, down45 * radiusMoveLong);
    67.  
    68.         bool cantMoveForward = //Stop player from pushing objects
    69.         (Physics.Raycast(playerMoveDiagonalRight, out hitForward, radiusMove)) && (hitForward.collider.CompareTag("Environment"))
    70.         || (Physics.Raycast(playerMoveDiagonalLeft, out hitForward, radiusMove)) && (hitForward.collider.CompareTag("Environment"))
    71.         || (Physics.Raycast(playerMoveDiagonalUp, out hitForward, radiusMoveLong)) && (hitForward.collider.CompareTag("Environment"))
    72.         || (Physics.Raycast(playerMoveDiagonalDown, out hitForward, radiusMoveLong)) && (hitForward.collider.CompareTag("Environment"))
    73.         || (Physics.Raycast(playerMoveDiagonalRight, out hitForward, radiusMove)) && (hitForward.collider.CompareTag("Objective"))
    74.         || (Physics.Raycast(playerMoveDiagonalLeft, out hitForward, radiusMove)) && (hitForward.collider.CompareTag("Objective"))
    75.         || (Physics.Raycast(playerMoveDiagonalUp, out hitForward, radiusMoveLong)) && (hitForward.collider.CompareTag("Objective"))
    76.         || (Physics.Raycast(playerMoveDiagonalDown, out hitForward, radiusMoveLong)) && (hitForward.collider.CompareTag("Objective"))
    77.         || (Physics.Raycast(playerMoveDiagonalRight, out hitForward, radiusMove)) && (hitForward.collider.CompareTag("NPC"))
    78.         || (Physics.Raycast(playerMoveDiagonalLeft, out hitForward, radiusMove)) && (hitForward.collider.CompareTag("NPC"))
    79.         || (Physics.Raycast(playerMoveDiagonalUp, out hitForward, radiusMoveLong)) && (hitForward.collider.CompareTag("NPC"))
    80.         || (Physics.Raycast(playerMoveDiagonalDown, out hitForward, radiusMoveLong)) && (hitForward.collider.CompareTag("NPC"));
    81.  
    82.         if ((Physics.Raycast(playerMoveDiagonalRight, out hitTarget, talkRadius))
    83.         || (Physics.Raycast(playerFront, out hitTarget, talkRadius))
    84.         || (Physics.Raycast(playerMoveDiagonalLeft, out hitTarget, talkRadius)))
    85.         {
    86.             if (hitTarget.collider.CompareTag("NPC"))
    87.             {
    88.                 target = hitTarget.transform;
    89.             }
    90.         }
    91.  
    92.         crouching = Input.GetButton("Crouch")
    93.         || (Physics.Raycast(playerTop, out hitTop, crouchDistance)); //keeps player slow under objects
    94.  
    95.         float speed = ((crouching) ? c_speed : //Crouching
    96.         (cantMoveForward) ? no_speed : //Hitting a Wall
    97.         w_speed) * moveRot.magnitude; //Normal Moving
    98.         transform.Translate(moveRot * speed * Time.deltaTime, Space.World); //Movement
    99.  
    100.         if (Input.GetButtonDown("Attack"))
    101.         {
    102.             launchAttack(attackHitBoxes[0]);
    103.         }
    104.  
    105.         if (controller.isGrounded)
    106.         {
    107.             if (isTalking)
    108.             {
    109.                 horizontalAxis = 0;
    110.                 verticalAxis = 0;
    111.                 FaceTarget();
    112.             } else
    113.             {
    114.                 target = null;
    115.             }
    116.  
    117.             moveDir = new Vector3(horizontalAxis, 0.0f, verticalAxis); //gets input from controller and keyboard
    118.             moveRot = moveDir.normalized;
    119.             if (canMove)
    120.             {
    121.                 if (moveDir != Vector3.zero)
    122.                 {
    123.                     playerMoving = true;
    124.                     lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
    125.                     if (target == null)
    126.                     {
    127.                         transform.rotation = Quaternion.LookRotation(moveRot); //keeps player facing direction
    128.                     }
    129.                 }
    130.  
    131.                 if ((Input.GetButtonDown("Jump")) && (!crouching) && (canJumpForward)) //prevents jumping in crouch
    132.                 { //Jumping
    133.                     moveDir.y = jumpForce;
    134.                     jumpAudio.Play();
    135.                 }
    136.  
    137.                 if (Input.GetButton("Crouch"))
    138.                 { //crouching
    139.                     controller.height = crouchingHeight;
    140.                 }
    141.                 else
    142.                 {
    143.                     controller.height = standardHeight;
    144.                 }
    145.                 if (Physics.Raycast(playerTop, out hitTop, crouchDistance))
    146.                 {
    147.                     if (hitTop.collider.CompareTag("Environment"))
    148.                     {
    149.                         controller.height = crouchingHeight;  //keeps player crouched under objects
    150.                     }
    151.                 }
    152.                 spriteAnimation.SetFloat("moveX", Input.GetAxisRaw("Horizontal"));
    153.                 spriteAnimation.SetBool("playerMoving", playerMoving);
    154.                 spriteAnimation.SetFloat("lastMoveX", lastMove.x);
    155.             }
    156.         }
    157.         else
    158.         {
    159.             if (moveDir.y < 0) //Influence Falling Direction
    160.             {
    161.                 moveDir.x = Input.GetAxis("Horizontal") * speed / 2;
    162.                 moveDir.z = Input.GetAxis("Vertical") * speed / 2;
    163.             }
    164.         }
    165.         moveDir.y -= gravity * Time.deltaTime;  //puts gravity on the capsule
    166.         controller.Move(moveDir * Time.deltaTime);
    167.     }
    168.  
    169.     public void FaceTarget()
    170.     { //To face the player when they're near
    171.         Vector3 direction = (target.position - transform.position).normalized;
    172.         Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
    173.         transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 4f);
    174.     }
    175.  
    176.     private void launchAttack(Collider col)
    177.     {
    178.         Collider[] cols = Physics.OverlapBox(col.bounds.center, col.bounds.extents, col.transform.rotation, LayerMask.GetMask("Hitbox"));
    179.         foreach(Collider c in cols)
    180.         {
    181.             if (c.transform.root == transform)
    182.             {
    183.                 continue;
    184.  
    185.             }
    186.             float damage = 0;
    187.             switch (c.name)
    188.             {
    189.                 case "enemyHitBox":
    190.                 damage = 10;
    191.                 break;
    192.                 default:
    193.                 Debug.Log("Unable to identify body part. Make sure collider matches name.");
    194.                 break;
    195.             }
    196.  
    197.             c.SendMessageUpwards("takeDamage", damage);
    198.         }
    199.     }
     
  2. Syganek

    Syganek

    Joined:
    Sep 11, 2013
    Posts:
    85
    Hi!

    Have you managed to fix this?

    I have almost exactly the same issue. I've tried changing slope limit, stepping offset, fiddled with Physics setting and nothing helped.
     
  3. Person96

    Person96

    Joined:
    Apr 16, 2018
    Posts:
    2
    I've just updated Unity and it fixed itself. Not sure if it was just the Engine itself or something.