Search Unity

Changing character Controller to a box collider

Discussion in 'Scripting' started by TheRaider, Jan 25, 2011.

Thread Status:
Not open for further replies.
  1. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    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.
     
    Last edited: Jan 25, 2011
  2. Shardz

    Shardz

    Joined:
    Dec 30, 2010
    Posts:
    42
    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.
     
    Last edited: Jan 25, 2011
  3. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    I can't do that with the charactercontroller or i can't accesss the move/simplemove functionality.
     
  4. Shardz

    Shardz

    Joined:
    Dec 30, 2010
    Posts:
    42
    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. :)
     
  5. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Actually, I think the character controller is hard-coded to use a capsule collider. (Unless that's changed recently.)
     
  6. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    yeah if i could simply change it in the script I would.

    I don't think you can even rotate the capsule right?
     
  7. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    That's right.
     
  8. Shardz

    Shardz

    Joined:
    Dec 30, 2010
    Posts:
    42
    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.
     
  9. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    the thing is all I want is the standard controller with a different collider :)

    What did you use to move instead of simplemove/move?
     
  10. Shardz

    Shardz

    Joined:
    Dec 30, 2010
    Posts:
    42
    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).
     
  11. AMO_Noot

    AMO_Noot

    Joined:
    Aug 14, 2012
    Posts:
    433
    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.
     
    Ignacii and ErnestoBananez like this.
  12. Zibber

    Zibber

    Joined:
    Jun 22, 2013
    Posts:
    86
    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.
     
  13. AMO_Noot

    AMO_Noot

    Joined:
    Aug 14, 2012
    Posts:
    433
    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.
     
  14. Zibber

    Zibber

    Joined:
    Jun 22, 2013
    Posts:
    86
    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?
     
  15. AMO_Noot

    AMO_Noot

    Joined:
    Aug 14, 2012
    Posts:
    433
    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.
     
    ErnestoBananez likes this.
  16. Zibber

    Zibber

    Joined:
    Jun 22, 2013
    Posts:
    86
    I think you will have to ditch the CharacterController if it is really an issue and use a RigidBody with raycasts.
     
  17. AMO_Noot

    AMO_Noot

    Joined:
    Aug 14, 2012
    Posts:
    433
    Bummer. Thanks for the help, regardless.
     
  18. Zibber

    Zibber

    Joined:
    Jun 22, 2013
    Posts:
    86
    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:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class CCMove : MonoBehaviour
    5. {
    6.     public float speed = 5.0f;
    7.  
    8.     private CharacterController controller;
    9.  
    10.     // Use this for initialization
    11.     void Start()
    12.     {
    13.         controller = GetComponent<CharacterController>();
    14.         if (controller == null)
    15.         {
    16.             Debug.LogError("Missing required CharacterController on " + this);
    17.         }
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         Vector3 moveDirection = Vector3.zero;
    24.  
    25.         if (Input.GetKey(KeyCode.D))
    26.         {
    27.             // Move Left
    28.             moveDirection = transform.forward;
    29.         }
    30.         else if (Input.GetKey(KeyCode.A))
    31.         {
    32.             // Move Right
    33.             moveDirection = -transform.forward;
    34.         }
    35.  
    36.         // How much to offset my raycasts from center of controller
    37.         Vector3 capsuleOffset = new Vector3(controller.radius, 0, 0);
    38.  
    39.         Ray centerRay = new Ray(transform.position, -Vector3.up); // Ray down the center of controller
    40.         Ray frontRay = new Ray(transform.position + capsuleOffset, -Vector3.up); // Ray down the outside front of controller
    41.         Ray backRay = new Ray(transform.position - capsuleOffset, -Vector3.up); // Ray down the outside back of controller
    42.  
    43.         float rayLength = (controller.height / 2) + 0.1f; // Ray starts at middle of controller, so half the height + some extra for the skin, etc
    44.  
    45.         // Check center, front, and back to see if they are all grounded before applying gravity
    46.         if (!Physics.Raycast(centerRay, rayLength))
    47.         {
    48.             if (!Physics.Raycast(frontRay, rayLength))
    49.             {
    50.                 if (!Physics.Raycast(backRay, rayLength))
    51.                 {
    52.                     moveDirection += Physics.gravity; // This is wrong, should be Physics.gravity per second
    53.                 }
    54.             }
    55.         }
    56.  
    57.         // Finally move
    58.         controller.Move(moveDirection * speed * Time.deltaTime);
    59.  
    60.         // Debug draw my raycasts
    61.         Debug.DrawRay(transform.position, -transform.up * rayLength, Color.white);
    62.         Debug.DrawRay(transform.position + capsuleOffset, -transform.up * rayLength, Color.white);
    63.         Debug.DrawRay(transform.position - capsuleOffset, -transform.up * rayLength, Color.white);
    64.     }
    65. }
    66.  
     
  19. LeetRooster

    LeetRooster

    Joined:
    Jul 20, 2013
    Posts:
    18
    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?
     
  20. iamsteele

    iamsteele

    Joined:
    Mar 25, 2014
    Posts:
    29
    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 =/
     
  21. forestrf

    forestrf

    Joined:
    Aug 28, 2010
    Posts:
    231
    Add 2 extra years
     
  22. forestrf

    forestrf

    Joined:
    Aug 28, 2010
    Posts:
    231
    Here says that there is a box character controller, but it also says physx 3.3.4 while I think that Unity uses 3.3.3 right now.
    If it is not a lot of pain, this would be very nice to have because math and ground detection is easier with boxes.

    (As a side note, GMod and maybe the rest of Source Engine games, use a box collider for the characters)
     
  23. doomlazy

    doomlazy

    Joined:
    Aug 24, 2013
    Posts:
    44
    I tried a box collider with a rigidbody and noticed that if you walk up to a wall and turn the wall pushes you out because eventually the box corners intersect the wall. Any idea how the source games work around this?
     
    420BlazeIt likes this.
  24. forestrf

    forestrf

    Joined:
    Aug 28, 2010
    Posts:
    231
    I am sure that they don't rotate the box collider. Instead I think that they rotate what is inside (at least, that is what I am doing right now)
     
  25. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    So you are only rotating your player model?
     
  26. forestrf

    forestrf

    Joined:
    Aug 28, 2010
    Posts:
    231
    I have the model inside a gameobject that its only purpose is to rotate. So I have the collider, inside the rotator, and inside the player model and other gameobjects
     
  27. maya-papaya

    maya-papaya

    Joined:
    Feb 24, 2015
    Posts:
    2
    This is extreme necroposting, but Im glad I found this thread. I have the same problem as shards, I'm making a space game that has no "down" except for the gravity of tiny planets. I might as well go with what zibber said and use a rigidbody, since the game is going to be physics-based anyway
     
  28. sicklepickle

    sicklepickle

    Joined:
    Apr 8, 2012
    Posts:
    44
    Since it's already been necro'd... Yeah the lack of shape options and rotation options are still doing my head in.
     
    EZaca likes this.
  29. Fhat_Videos

    Fhat_Videos

    Joined:
    Aug 12, 2020
    Posts:
    1
    They really haven't added any shape options nearly 10 years later... unreal.
     
  30. DannyRay

    DannyRay

    Joined:
    Jul 17, 2020
    Posts:
    5
    Add another year :/ Guess I gotta redo my movement system now ._.
     
  31. dominosinco

    dominosinco

    Joined:
    Apr 23, 2019
    Posts:
    2
    warning please do not click the unify link would be a good idea to just ignore it.
     
  32. AleMarcati

    AleMarcati

    Joined:
    Jan 4, 2022
    Posts:
    9
    Is there a way to edit the script of a prefab component like Character Controller?
    I mean, get a copy of the script and edit that? I'm looking for a way to create my own Character Controller component, but I don't want to start from scratch...
     
  33. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    AleMarcati likes this.
Thread Status:
Not open for further replies.