Hello I am trying to add a character controller to a fox character, currently I am using the Character controller but as this is capsule shaped it does not work for my character, is there a way I can change the shape of the controller, flip it or connect a box collider to the controller (I’m sorry for the question I’m new to Unity)
Unfortunately, the CharacterController is always an upright capsule collider - you can’t replace it with a box collider or horizontal capsule, nor add other colliders to it: the CharacterController “thinks” that it’s colliding with the other colliders, and stands still or moves to stupid directions.
The best you can do with a CharacterController is to adjust its height and radius so that it becomes a sphere (radius = height/2). If this doesn’t work fine in your case, a possible solution is to use a Rigdbody character: try a box collider with the wiki script RigidbodyFPSWalker - this script requires a capsule collider, but you can remove this restriction like below:
var speed = 10.0;
var gravity = 10.0;
var maxVelocityChange = 10.0;
var canJump = true;
var jumpHeight = 2.0;
private var grounded = false;
// capsule collider removed from this line:
@script RequireComponent(Rigidbody)
function Awake ()
{
rigidbody.freezeRotation = true;
rigidbody.useGravity = false;
}
function FixedUpdate ()
{
if (grounded)
{
// Calculate how fast we should be moving
var targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
targetVelocity = transform.TransformDirection(targetVelocity);
targetVelocity *= speed;
// Apply a force that attempts to reach our target velocity
var velocity = rigidbody.velocity;
var velocityChange = (targetVelocity - velocity);
velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
velocityChange.y = 0;
rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
// Jump
if (canJump && Input.GetButton("Jump"))
{
rigidbody.velocity = Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
}
}
// We apply gravity manually for more tuning control
rigidbody.AddForce(Vector3 (0, -gravity * rigidbody.mass, 0));
grounded = false;
}
function OnCollisionStay ()
{
grounded = true;
}
function CalculateJumpVerticalSpeed ()
{
// From the jump height and gravity we deduce the upwards speed
// for the character to reach at the apex.
return Mathf.Sqrt(2 * jumpHeight * gravity);
}