Changing character Controller to a box collider

i would really rather use a box collider to see if my character is hitting things. Is there anyway to change the capsule for this?

I also tried making a gameobject with a box collider in it and making it a child and that didn’t go so well.

Also is there a way to manually reposition the collider. I don’t really like how it is position and would like to reposition it with a mouse.

You can delete the Box Collider component of your character in the Inspector and then go to the Component menu and add any other shape you wish. To adjust its’ relative position to the character, there should be a Center option (along with Scale, etc.) for it over in the Inspector to get it fitting snug and nice. I hope this answers your question.

P.S. I should also note that using the Mesh Colider should only be used for static GameObjects and aren’t well intended for Characters which move around frequently.

I can’t do that with the charactercontroller or i can’t accesss the move/simplemove functionality.

You are using one of the prefab Character Controllers which ships with Unity then I assume. In which case, you answered your own question. Somewhere in their script, it calls for the Capsule Collider by default and I’m assuming that can be modified as per usual. I ended up going rogue and scripted my own controller and avoided theirs because 1.) I wanted to learn faster and 2.) my character would never fit in that capsule. :slight_smile:

Actually, I think the character controller is hard-coded to use a capsule collider. (Unless that’s changed recently.)

yeah if i could simply change it in the script I would.

I don’t think you can even rotate the capsule right?

That’s right.

Yes, Jesse is indeed correct. I jumped into this thread thinking the controller in question was custom, not one of the Unity prefabs. Your best bet at this point is to search around for key words regarding the type of character you want + controller…or jump on the Wiki or search this site and piece together a custom controller that suits your needs better. I was kind of in the same boat two weeks ago, but I found the joys and benefits of manual labor…even though it got a bit dirty.

the thing is all I want is the standard controller with a different collider :slight_smile:

What did you use to move instead of simplemove/move?

I went straight to a space theme, slapped a Box Collider on my spaceship, then proceeded to comb the Wiki, Forum, and Docs pages for the information to make it work. There are hundreds upon hundreds of example controllers here on the site…I’m even sure I saw one for a flying tomato (I’m sure that was a Sphere Collider).

Ancient thread, I know. But still pretty relevant, as Unity still forces capsule collision on the character controller.

Does anybody have suggestions/recommendations/workarounds for this? The capsule collider is a big problem for 2D games and platform edges.

2 Likes

You can use a kinematic rigidbody; using the AddForce and MovePosition functions to move. If you don’t want it to tumble, you can lock axis’. To check for isGrounded, you can do a Raycast downward a short distance, if it hits anything, then you are grounded.

Thanks; there’s no way to salvage the default Character controller though? Minus this issue, we’d gotten movement pretty much perfect, would seem a shame to start from scratch.

I’m assuming the issue you are running into is that the rounded edges on the bottom of the capsule collider can cause the character to interact weirdly or slide with edges of a box collider?

Correct!

For a 2D game, platformers in particular, you really need that precise collision on platform edges.

http://www.youtube.com/watch?v=gA8Ns_KMxv0

Here’s another user from years ago with the same issue.

1 Like

I think you will have to ditch the CharacterController if it is really an issue and use a RigidBody with raycasts.

Bummer. Thanks for the help, regardless.

So, I was thinking about this more and I found a solution if you are going 2D. It isn’t exactly pretty, but it works. Essentially, you will have to cast 3 Raycasts; 1 down the center of the controller, 1 down the outside of the front of the controller, and 1 down the outside of the back of the controller. If all 3 don’t hit the ground, then apply gravity. Otherwise, do not apply gravity.

Some sample code:

using UnityEngine;

public class CCMove : MonoBehaviour
{
    public float speed = 5.0f;

    private CharacterController controller;

    // Use this for initialization
    void Start()
    {
        controller = GetComponent<CharacterController>();
        if (controller == null)
        {
            Debug.LogError("Missing required CharacterController on " + this);
        }
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 moveDirection = Vector3.zero;

        if (Input.GetKey(KeyCode.D))
        {
            // Move Left
            moveDirection = transform.forward;
        }
        else if (Input.GetKey(KeyCode.A))
        {
            // Move Right
            moveDirection = -transform.forward;
        }

        // How much to offset my raycasts from center of controller
        Vector3 capsuleOffset = new Vector3(controller.radius, 0, 0);

        Ray centerRay = new Ray(transform.position, -Vector3.up); // Ray down the center of controller
        Ray frontRay = new Ray(transform.position + capsuleOffset, -Vector3.up); // Ray down the outside front of controller
        Ray backRay = new Ray(transform.position - capsuleOffset, -Vector3.up); // Ray down the outside back of controller

        float rayLength = (controller.height / 2) + 0.1f; // Ray starts at middle of controller, so half the height + some extra for the skin, etc

        // Check center, front, and back to see if they are all grounded before applying gravity
        if (!Physics.Raycast(centerRay, rayLength))
        {
            if (!Physics.Raycast(frontRay, rayLength))
            {
                if (!Physics.Raycast(backRay, rayLength))
                {
                    moveDirection += Physics.gravity; // This is wrong, should be Physics.gravity per second
                }
            }
        }

        // Finally move
        controller.Move(moveDirection * speed * Time.deltaTime);

        // Debug draw my raycasts
        Debug.DrawRay(transform.position, -transform.up * rayLength, Color.white);
        Debug.DrawRay(transform.position + capsuleOffset, -transform.up * rayLength, Color.white);
        Debug.DrawRay(transform.position - capsuleOffset, -transform.up * rayLength, Color.white);
    }
}

This seems like a mistake in the API, a coupling issue with the capsule collider and the character controller. Is there a feature request tracking this for a future unity release? I’m a bit new to the community what does it take to file a feature request?

really wish the capsule collider could be swapped out for box collider, etc. Glad to see it’s still not been fixed 3 years later =/