- Home /
How to add diving/rolling to FPSWalker script? (Not animation, just speed)
I dunno why I can't figure this out, but its really frustrating me. I am trying to add a feature to the FPSWalker script so that if the player taps the spacebar key while moving, he will "dive" or "roll" in that direction, momentarily increasing his speed in that direction for a brief moment.
How would I go about doing this? I tried doing something similar to the stock jump code in the FPSWalker script, and tried starting a timer sort of like the bullet reset from the machine gun/ rocket launcher from the rocket launcher/machinegun script from the FPS tut, and applying the speed over that time, but I can't get it to do anything.
Answer by Heratitan · Aug 06, 2010 at 05:05 PM
You could try doing it almost completely the same way as jumping by changing the axis on which you jump to the z axis, so that you jump forward. You could then increase the speed of the dive so that it is faster than the walking speed. You would also have to adjust the gravity so that you stopped after a few feet.
For Example:
var speed = 6.0; var diveSpeed = 40.0; var gravity = 20.0;
private var moveDirection = Vector3.zero; private var grounded : boolean = false;
function FixedUpdate() { if (grounded) { // We are grounded, so recalculate movedirection directly from axes moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.z = diveSpeed;
}
}
// 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;
}
This is the FPS Walker from the FPS Tutorial I think, except with the dive. Hope That helps.
This is what I tried, but unlike jumping on the y axis (because gravity stops the speed increase), jumping on the z axis will just jump indefinitely, like a run mode. What do you mean when you say "adjust the gravity so you stopped after a few feet"? How would I go about doing that?
Answer by PrimeDerektive · Aug 06, 2010 at 06:58 PM
Not sure if its poor form to answer your own question, but I think I figured this out.
I wanted to be able to dive in any direction, so I created a coroutine called DiveCheck(), that checks for a press of the jump button, and if pressed, changes walkSpeed to diveSpeed, waits for .1 second, and changes speed back to its original value (so its only a quick burst). Then I do another WaitForSeconds(0.5), so you can't spam the dive key.
Heres my code (i took out any vertical jumping code, because my game does not allow jumping):
var speed = 6.0; var diveSpeed = 20.0;
private var moveDirection = Vector3.zero;
function Start(){ DiveCheck(); }
function FixedUpdate() {
//Calculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
controller.Move(moveDirection * Time.deltaTime); }
function DiveCheck(){ while(true){
var initialSpeed = speed;
if(Input.GetButtonUp("Jump")){
speed = diveSpeed;
yield WaitForSeconds(0.1);
speed = initialSpeed;
yield WaitForSeconds(0.5);
}
yield;
}
}
Is it just me, or is figuring stuff out on your own in Unity one of the most gratifying things in the world? :)