I’ve added a character controller to a capsule and even though it has a rigidbody attached, it won’t use gravity and fall to the floor. As soon as I disable it though, it does. I even tried making artificial gravity through scripting (the script uses flags to detect whether or not the player is touching the floor):
Vector3 moveDir = Vector3.zero;
void Update () {
moveDir.y -= gravity * Time.deltaTime;
var flags = controller.Move(moveDir * Time.deltaTime);
grounded = ((flags & CollisionFlags.CollidedBelow) != 0);
}
(‘controller’ being the CharacterController)
That didn’t even work. Why is this happening?
Edit
Here’s the whole movement script:
public float speed = 10;
public float rotationSpeed = 2;
private bool canRun = true;
public float runSpeed = 15;
public bool running;
private bool canCrawl = true;
public float crawlSpeed = 5;
public bool crawling;
public float standUpTime = 3;
public float crawlHeight;
private float normalHeight;
private Vector3 tempCent;
private bool canJump = true;
public float jumpHeight = 5;
public bool jumping;
public float gravity = 1;
private Vector3 moveDir;
private Vector3 rotDir;
private float moveHor;
private CharacterController controller;
private PlayerStats playerStats;
private Transform player;
void Awake () {
player = transform;
controller = player.GetComponent<CharacterController>();
playerStats = player.GetComponent<PlayerStats>();
normalHeight = controller.height;
tempCent.y = controller.center.y/2;
}
void Update () {
bool isControllable = playerStats.isControllable;
moveDir.y -= gravity * Time.deltaTime;
if(!isControllable){
Input.ResetInputAxes();
}else{
if(controller.isGrounded){
moveDir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDir = player.TransformDirection(moveDir);
moveDir *= speed;
moveHor = Input.GetAxis("Horizontal");
if(!crawling && !jumping){
canRun = playerStats.canRun;
running = playerStats.canRun;
}
canJump = playerStats.energy > 0;
if(moveHor > 0){
rotDir = new Vector3(0, 1, 0);
}else if(moveHor < 0){
rotDir = new Vector3(0, -1, 0);
}else{
rotDir = new Vector3(0, 0, 0);
}
if(Input.GetButton("Jump")){
if(canJump){
moveDir.y = jumpHeight;
canRun = false;
canCrawl = false;
controller.height = normalHeight;
controller.center = tempCent;
}
}
if(Input.GetKey(KeyCode.LeftShift)){
if(canRun){
moveDir *= runSpeed;
canCrawl = false;
running = true;
}
if(crawling){
animation.Play("GetUp");
canRun = true;
crawling = false;
controller.height = normalHeight;
controller.center = tempCent;
}
}else{
running = false;
}
if(Input.GetKey("c")){
if(canCrawl){
moveDir *= crawlSpeed;
controller.height = crawlHeight;
tempCent.y -= 0.25F;
controller.center = tempCent;
canRun = false;
canJump = false;
crawling = true;
}
}else{
crawling = false;
}
controller.Move(moveDir * Time.deltaTime);
controller.transform.Rotate(rotDir * Time.deltaTime, rotationSpeed);
}
moveDir.y -= gravity * Time.deltaTime;
}
}
}