- Home /
CharacterController - moving while jumping (movement vector values reset?)
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);
}
}
Answer by EssyTech · Sep 12, 2015 at 07:12 AM
Input.GetButton will only execute the code inside of its block if the button is held down. Are you just tapping the button or holding the button? Also, you should run your input commands in update not fixed update because update gets run every frame were fixed update does not.
Originally I had GetButtonDown but I changed it to GetButton so I could hold the input and see what was going on with the jumping.
The movementVector's x and z are still unexplainedly modified when it's GetButtonDown, the visual effect of interrupting movement is just less noticeable (still there).
$$anonymous$$oving the jumping input to Update() doesn't address the actual problem, neither of the issues you discussed do.
The comment you put were is said the the code for not being grounded is not relevent could be were the issue lies. If the player is not grounded, hence they are jumping, (isGrounded is probably false) then the code that you put in the not grounded section will be executing. Need to look there. Since its not present here, I don't no how else to help.
Thanks for the replies I actually fixed this earlier today, you are correct the Else of the isGrounded check was one part of movementVector being messed with.