Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Siggie1 · Aug 28, 2014 at 02:57 PM · double-tap

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; 
 }








Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Siggie1 · Aug 28, 2014 at 04:13 PM 0
Share

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; 
 }
avatar image llnk7 · Aug 28, 2014 at 06:39 PM 0
Share

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!

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges