- Home /
High speed and collisions: Character Controller falls through the ground when jumping\running at a certain speed
I know, maybe this is one of the most FAQ here. I tried to find myself a solution but simply can't easily figure how to deal with this problem (at least, for my experiment).
Just for the sake of prototyping, I'm using a First Person Controller-shaped (the capsule) Third Person Controller, who has to run on the X axis and jump while running over a Z=30 rotated surface.
This is the Controller script (which I took from these tutorials) attached to my character and the following is the script (slightly modified version of the best answer to this question) that makes my character accelerate while running:
var defaultSpeed : float = 50.0f; // speed to default towards if no keys are pressed
var maxSpeed : float = 75.0f;
var minSpeed : float = 25.0f;
var maxAcceleration : float = 5.0f; // Controls the acceleration
var currentSpeed : float = 50.0f;
var target : Transform;
function Update()
{
transform.Translate(Vector3.forward * Time.deltaTime * currentSpeed);
if(Input.GetKey(KeyCode.A)) { // I'm using both A and S keys to let my character run
currentSpeed += maxAcceleration * Time.deltaTime;
} else if(Input.GetKey(KeyCode.S)) {
currentSpeed += maxAcceleration * Time.deltaTime;
} else if (currentSpeed > defaultSpeed) {
currentSpeed -= maxAcceleration * Time.deltaTime;
currentSpeed = Mathf.Clamp(currentSpeed, defaultSpeed, maxSpeed);
} else {
currentSpeed += maxAcceleration * Time.deltaTime;
currentSpeed = Mathf.Clamp(currentSpeed, minSpeed, defaultSpeed);
}
currentSpeed = Mathf.Clamp(currentSpeed, minSpeed, maxSpeed);
}
I've put a float counter on my GUI to keep easily track of the character's speed and, when he reaches - more or less - 30, ghosts through the surface, falling. This happens even if the character jumps too much (like he carves himself a hole in the surface).
I've even attached a Rigidbody to the controller, checking the Gravity and Kinematic checkboxes, which, at least, avoids the "earthquake" effect I got before :P
Is there something I can do?
Thanks a million :)