Unity : OK to jump but impossible to bounce
Hi,
here a piece of code that make my character jump with gravity. It works only if i push the J key. Impossible to make him jump automatically when he touch the ground. I try everything with no success.For example, i try a moveDirection.y = jumpSpeed when the character collide with my cube but nothing append. In the update function, i try to delete the if condition by pressing the J key with only moveDirection.y = jumpSpeed; : the character jump only one time and stay at the ground.
Thanks in advance !
here a piece of code that make my character jump with gravity. It works only if i push the J key. Impossible to make him jump automatically when he touch the ground. I try everything with no success.For example, i try a moveDirection.y = jumpSpeed when the character collide with my cube but nothing append. In the update function, i try to delete the if condition by pressing the J key with only moveDirection.y = jumpSpeed; : the character jump only one time and stay at the ground.
Thanks in advance !
var speed: float = 5.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.GetKey(KeyCode.J)) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
function OnControllerColliderHit (hit : ControllerColliderHit){
if(hit.gameObject.name == "Cube"){
moveDirection.y = jumpSpeed;
} }
