- Home /
CharacterController Physics
Hello There
I would like to, as i've seen it in many other scripts, make my character jump when he is grounded. But, my character, doesn't even reaches the ground, i changed my script and it doesn't move now (nasty little cube). There is a Big collsion mystery i my game. I gave my guy a Character controller, my floor and platforms box colliders. Am i missing something ? Sorry to abuse your generosity : The script :
var speed = 1.0; var jumpspeed = 1.0; var gravity = 20.0;
private var collider : CharacterController;
function Update () {
if (collider.isGrounded) {
var x = Input.GetAxis("Horizontal"); transform.Translate(x speed Time.deltaTime, 0, 0);
if(Input.GetButton("Jump")) {
var jump = jumpspeed * Time.deltaTime;
transform.Translate(0, jump * gravity, 0);
}
}
}
@script RequireComponent (CharacterController)
Answer by skovacs1 · Oct 21, 2010 at 06:19 PM
The CharacterController will not generate collisions if you move the transform and if it does not have a Collider.
You need to move the CharacterController with Move or SimpleMove.
collider is an already defined property on your script for the object's Collider and what's more you never assign it before you use it. Collider does not have an isGrounded property while CharacterController does.
You're multiplying your jump by gravity? What? Doesn't gravity usually subtract from jumping rather than multiply?
From the docs on ControllerCollider.Move:
var speed : float = 6.0; var jumpSpeed : float = 8.0; var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
function Update() { var controller : CharacterController = GetComponent(CharacterController); if (controller.isGrounded) { // We are grounded, so recalculate // move direction directly from axes moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= speed;
if (Input.GetButton ("Jump")) moveDirection.y = jumpSpeed;
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
@script RequireComponent (CharacterController)