Search Unity

Character Controller, Step Offset, and moving through gaps

Discussion in 'Scripting' started by drizztmainsword, Sep 24, 2013.

  1. drizztmainsword

    drizztmainsword

    Joined:
    Sep 4, 2012
    Posts:
    3
    Okay, so I have a character who's collision is handled by a CharacterController. The CharacterController's height is 1.7, its radius is .3, its skin width is 0.08, and its step offset is .5. Based on these figures, I would expect the character to be able to walk through a 1-unit wide, 2-unit tall gap with ease. However, it cannot, and the culprit appears to be the step offset. With it set to zero, the character can navigate this gap correctly.

    So, is there a known workaround for this issue? I can't set Step Offset to zero, as I need to be able to go up objects that are .5 units tall. There are three possible solutions that I can think of, and I'd really appreciate some input on which one is the best or if there is a method I haven't thought of.

    1) Use a trigger collider or a spherecast around the top of the character to try and detect a ceiling and turn the step offset to zero.

    2) Set the Step Offset to zero and use a trigger collider or a spherecast at the bottom of the character to try and detect an object to step onto and then set the step offset to .5.

    3) Leave the Step Offset at 0 at all times and use an alternative method of stepping over .5-unit-high obstacles.

    Any input would be greatly appreciated, thank you!
     
  2. Yofurioso

    Yofurioso

    Joined:
    Apr 9, 2013
    Posts:
    4
    Of the top of my head, I would leave Step Offset at 0, then raycast forward for 1 unit at 0.125 height to detect any possible step (design your game to avoid steps lower than 0.125 height). If a step is found adjust the value of Step Offset as desired, otherwise set it to 0; a simple "if...else" should do the trick.
     
  3. drizztmainsword

    drizztmainsword

    Joined:
    Sep 4, 2012
    Posts:
    3
    Hey, thanks for the reply.

    Half-unit steps are non-optional in this instance. In any case, I found a decent workaround. I have a sphere cast that starts near the player's head and then goes upwards and in the direction the player is moving. When this sphere hits an obstruction, it sets the Step Offset to the difference between the obstruction and the Character Controller's normal height. This mechanic also lets me determine the maximum hight a character can be at, letting me do things like have the player automatically crouch and stay crouched in low spaces.

    I've also sent a bug report about the Character Controller behavior into Unity, and they've accepted it. I obviously don't know if or when it will be fixed, but the team is at least aware of it now.
     
  4. Tsilliev

    Tsilliev

    Joined:
    Jan 21, 2014
    Posts:
    34
    "I've also sent a bug report..."

    Well good luck with that, 5 years later and I stumbled on this issue just now accidentally, wasted half a day debugging to see why I can't go in the 2f tall, 1f wide gap.

    Maybe they will take notice from this bump.

    Thank you for the solution ideas.

    edit:
    My solution based on the idea above.

    1) Create a box collider that is a child of the character and make its scale 0.1.
    2) Put the child a little above and in front of the head of the character
    3) Attach this script to the child:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class StepChanger : MonoBehaviour {
    6.  
    7.     GameObject pawn; // parent - character
    8.     CharacterController controller;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.     pawn = this.gameObject.transform.parent.gameObject;
    13.     controller = pawn.GetComponent<CharacterController>();
    14.     controller.height = 1.8f;
    15.     controller.radius = 0.4f;
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.        
    21.     }
    22.    
    23.     void OnTriggerEnter(Collider other)
    24.     {
    25.        print("colliding with: " + other.gameObject.name);
    26.         if (other.gameObject.name.Contains("Block")) //the name of the object that is the ceilling
    27.         {
    28.         print("we hit ceiling dropping step to 0.1");
    29.         controller.stepOffset = 0.1f;
    30.              
    31.         }
    32.     }
    33.    
    34.      void OnTriggerExit(Collider other)
    35.     {
    36.        print("collision exit");
    37.         if (other.gameObject.name.Contains("Block")) //the name of the object that is the ceilling
    38.         {
    39.         print("collision exit, return step offset to 1.1f");
    40.         controller.stepOffset = 1.1f;
    41.              
    42.         }
    43.     }
    44. }
    45.  
     
    Last edited: Sep 30, 2018
  5. chrisrcisme

    chrisrcisme

    Joined:
    Jul 7, 2014
    Posts:
    14
    've only just found the reason why my player using character controller component was slowing down alot while going through a doorway even though there is about 0.5+ gap between top of capusule and doorway box collider was simply changing step offset from 0.3 or any number above 0 to 0.

    Took about 1 month break from unity till today as it frustrated me so much to not find out what was going wrong to now find out its a bug in the character controller component.

    Thanks for the answers here in the thread otherwise id be stuck for another month atleast!
     
  6. Jichaels

    Jichaels

    Joined:
    Dec 27, 2018
    Posts:
    142
    My god, after hours of debugging I also figured this out........ How can it not be fixed after 6 F***** months ??!
     
    chrisrcisme likes this.
  7. chrisrcisme

    chrisrcisme

    Joined:
    Jul 7, 2014
    Posts:
    14
    Atleast you found the bug faster than I did haha well done.