I have an autorunner movement script - I’m trying to get jumping while moving working with a CharacterController.
I’m seeing this odd behavior: when I jump, the X and Z members of the movementVector in the player script are reset to 0, even though I’m only setting the .y member to jumpSpeed. This means that when I jump, I stop moving and only go up in the Y direction for a bit then continue autorunning.
I found this thread that suggested I separate the vertical speed into another variable, so here’s that version of the movement script. The previous version just set movementVector.y = jumpSpeed directly when the Jump Input was pressed. Both versions result in the same behavior (stopping movement until jumping is over).
void FixedUpdate() {
if (controller.isGrounded) {
// Set movement vector to forward transform
movementVector = transform.forward;
// Apply player's speed
movementVector *= _movementProperties.playerSpeed;
// A grounded character has 0 vertical speed unless...
_movementProperties.vertSpeed = 0.0f;
// jump input is pressed
if (Input.GetButton("Jump")){
_movementProperties.vertSpeed = _movementProperties.jumpSpeed;
}else{
_playerProperties.jummping = false;
}
}else{
// REMOVED - There's a LOT of code here for when we aren't grounded, not relevant
}
}
// Apply gravity to vertical speed
_movementProperties.vertSpeed -= _movementProperties.gravity * Time.deltaTime;
// Assign vertical speed to movement vector
movementVector.y = _movementProperties.vertSpeed;
// Apply final movement vector
if (_playerProperties.aliveP){
controller.Move(movementVector * Time.deltaTime);
}
}