- Home /
Make the Character Controller jump to the side
Hi all, how would I go about adding a sideways jump to the FPSWalker script? For example, dodging to the left or right to avoid a collision. I've attempted it with the following but just get vertical jumping. Thanks.
[EDIT] Thanks to Steve pointing out that the FPSWalker script already does most of what I want! I am very forgetful sometimes.
I've gone back to that script with the addition of a boolean so that the player character can only dodge while fighting. There's still the issue of turning though, since the wolf will need to be able to turn to face the target (we're not using the mouse for turning, only orbiting the PC). It would be nice if the player character could rotate while on the ground, but when pressing the jump button and left/right, he jumps to the side without any rotation. If anyone has any ideas how I can amend the following script to accomodate that I'd be grateful.
if (grounded) { // We are grounded, so recalculate movedirection directly from axes if(isFighting){ moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); } else { moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical")); transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0); }
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= currentSpeed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}
Could you clarify a bit what you are looking for that differs from the default FPSWalker script behavior? In the default script the jump will already occur in whatever direction you are moving. I.e. If you are strafing to the left and press the jump button, you will jump to the left.
I see you've added some rotation code. Are you looking for some sort of jump plus turn behavior?
Ah..I didn't realise that!! it's been so long since I modified it. I'll go back to the original script and check that out. The turning is just for turning while on the ground (as opposed to strafing,, since that would be unrealistic for a wolf!) Will I still be able to keep my rotation (as a separate move) with the jumping?
Answer by SteveFSP · May 19, 2010 at 03:24 PM
It should take one simple adjustment to your script. Move the rotation down to where you check the jump. I checked it in my sandbox project and it appears to behave correctly.
[EDIT] Fixed the code to restore the rotation block while fighting. That adds an extra boolean check.
function FixedUpdate() {
if (grounded) {
var inputH : float = Input.GetAxis("Horizontal");
var inputV : float = Input.GetAxis("Vertical");
// We are grounded, so recalculate movedirection directly from axes
if (isFighting) {
moveDirection = new Vector3(inputH, 0, inputV);
} else {
// Don't rotate here.
moveDirection = new Vector3(0, 0, inputV);
}
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
} else {
if (!isFighting) {
// Only rotate when not jumping.
transform.Rotate(0, inputH * rotateSpeed * Time.deltaTime, 0);
}
}
}
// Apply gravity moveDirection.y -= gravity * Time.deltaTime;
// Move the controller var controller : CharacterController = GetComponent(CharacterController); var flags = controller.Move(moveDirection * Time.deltaTime); grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}
There is one weakness to the way you are doing it. If the character is rapidly bouncing up and down off the ground, such as can occur on rough ground, the rotation will be a bit jittery.
If that is a problem for you, you may want to mimic the smooth rotation method used by the standard MouseLook Script. I.e. Create a private method that implements the the rotation smoothly, and call the method in place of the direct call to "transform.Rotate(0, inputH rotateSpeed Time.deltaTime, 0);".
Hi Steve, thanks for taking the time to look at this, I really appreciate it. I had something like that a short while ago. Your suggestion does work, but the only problem is the character strafes and rotates while grounded in the isFighting condition. I would rather it just turned. Thanks for the heads up on the smoothing, i'll look at implementing that now too.
I think i'm making this harder than it needs to be. I haven't animated the dodge yet so i'm thinking rather than jumping to the side, a roll would be easier (I can just use strafing). I'll try that and see how it goes. $$anonymous$$any thanks again Steve.
Oops, missed that. I fixed the answer to restore the rotation block when fighting.
Your answer
Follow this Question
Related Questions
Why can't I properly jump? 2 Answers
Character Controller / Charactor Motor has no inputs? 1 Answer
How to make your character jump? 1 Answer
Can't jump while sprinting 1 Answer