- Home /
Double tap "w" to run (fps)
I really need some help. I am trying to create my first fps game and I need some help with the movement of the character. I have a script that can run by holding down left shift, but I want it to run when I double tap "w". The script I am using can crouch by pressing left control and that. But I cant figure out how to create a script where I can double tap w to run. I am pretty new to unity. If someone could help me i would be really happy. This is the script I've been using:
var walkSpeed: float = 7;
var crchSpeed: float = 3;
var runSpeed: float = 20;
private var chMotor: CharacterMotor;
private var ch: CharacterController;
private var tr: Transform;
private var height: float;
function Start(){
chMotor = GetComponent(CharacterMotor);
tr = transform;
ch = GetComponent(CharacterController);
height = ch.height;
}
function Update(){
var h = height;
var speed = walkSpeed;
if (ch.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift")){
speed = runSpeed;
}
if (Input.GetKey("left ctrl")){
h = 0.5 * height;
speed = crchSpeed;
}
chMotor.movement.maxForwardSpeed = speed;
var lastHeight = ch.height;
ch.height = Mathf.Lerp(ch.height, h, 5*Time.deltaTime);
tr.position.y += (ch.height-lastHeight)/2;
}
Answer by Doodlemeat · Aug 28, 2014 at 03:11 PM
Create a boolean variable called prepareFastRun = false Also create a timer. Also create a float variable called prepareFastRunTimeout and set it to like 1(second)
Then when you press the W button you must check if prepareFastRun is true. If it is true, then you have to check if the timer has passed the **prepareFastRunTimeout*. If it has passed, then you are ready to set the player speed to your fast run speed.
If it is false, you start the timer. That's everything you have to do.
Also when the player releases the W button you have to check if player IS running fast. If he is, then set his speed to the normal.
I have tried to edit my script, but when i press and hold the w key, the button count still keeps counting until i release w.
var walkSpeed: float = 7;
var crchSpeed: float = 3;
var runSpeed: float = 20;
var ButtonCooler : float = 0.5 ; // Half a second before reset
var ButtonCount : int = 0;
private var ch$$anonymous$$otor: Character$$anonymous$$otor;
private var ch: CharacterController;
private var tr: Transform;
private var height: float;
function Start(){
ch$$anonymous$$otor = GetComponent(Character$$anonymous$$otor);
tr = transform;
ch = GetComponent(CharacterController);
height = ch.height;
}
function Update(){
var h = height;{
var speed = walkSpeed;
if ( Input.Get$$anonymous$$ey ("w")){
if ( ButtonCooler > 0 && ButtonCount == 2/*Number of Taps you want $$anonymous$$inus One*/){
//Has double tapped
speed = runSpeed;
}else{
ButtonCooler = 0.5 ;
ButtonCount += 1 ;
}
}
if ( ButtonCooler > 0 )
{
ButtonCooler -= 1 * Time.deltaTime ;
}else{
ButtonCount = 0 ;
}
}
if (Input.Get$$anonymous$$ey("left ctrl")){
h = 0.5 * height;
speed = crchSpeed;
}
ch$$anonymous$$otor.movement.maxForwardSpeed = speed;
var lastHeight = ch.height;
ch.height = $$anonymous$$athf.Lerp(ch.height, h, 5*Time.deltaTime);
tr.position.y += (ch.height-lastHeight)/2;
}
mm I think it has to be with the differences between Get$$anonymous$$ey, Get$$anonymous$$eyDown and Get$$anonymous$$eyUp, What I would do is when is pressed the W (Get$$anonymous$$ey) you set the variable to true, and in the Get$$anonymous$$eyUp you start counting the time, if is pressed again get$$anonymous$$ey and the variable is true and the time is less than the $$anonymous$$ims time required then you can run. I hope I was clear and is useful what I'm saying. Good luck!
Answer by sixEight · Aug 28, 2014 at 03:14 PM
I agree with this. You can see a code example of the boolean variable here: http://answers.unity3d.com/questions/340593/how-do-i-make-a-double-tap-system-for-dashing.html
Your answer
Follow this Question
Related Questions
Joystick GUI activates objects. 1 Answer
codes for detecting mobile double tap and hold 1 Answer
Help with checking for a key tapped multiple times 1 Answer
Double jump with touch 1 Answer
Glitchy Double Tap to Run 1 Answer