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.
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.
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.
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).
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?
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?