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 Xatoku · Apr 09, 2011 at 06:21 AM · buttonrunwalkdouble-tap

Double Tap to Run

I basically want to double tap the arrow/a/d key to make my character run instead of walk. I have no clue how to even begin doing this, so I'm asking for someone to either do it for me or give me an example I can embellish on.

Originally, walkspeed = 30, I want it to be 50 when I double tap.

  function Update () {
   if(grounded) { 
        moveDirection = new Vector3(0,0,Input.GetAxis("Horizontal")); 
        transform.LookAt(transform.position + moveDirection); 
        if(isMoveable == true && !isAttacking){
            moveDirection *= walkSpeed;
        }
        if(moveDirection != Vector3.zero && isMoveable && !isAttacking){
            if (Time.time - lastTime < 0.2f){
                lastTime = Time.time;
                walkSpeed = 30;
                animation.CrossFade("run",0.1);
                isMoving = true;
            }else{
                lastTime = Time.time;
                walkSpeed = 50;
                animation.CrossFade("run",0.1);
                isMoving = true;
            }
        } else {
            isMoving = false;
            if(!lookingUp){
                animation.CrossFade("Take 001",0.1);
            }
            if(lookingUp){
                animation.CrossFade("idleUp",0.1);
            }
        }
        if(Input.GetButton("Jump") && !isJumping && !isShooting && !isAttacking) {
            Jump();
        }
    }

Comment
Add comment · Show 2
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 FLASHDENMARK · Apr 09, 2011 at 07:27 AM 0
Share

I would suggest starting a timer when you hit the arrow-key and if the timer is less than (let us say .50 seconds(half a second)) then make the character run.

avatar image Jessy · Apr 09, 2011 at 11:55 AM 0
Share

http://forum.unity3d.com/threads/8620-Double-Tapping-axis-input

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by TheDemiurge · Apr 09, 2011 at 07:33 AM

I'm assuming you plan to have the key held down for both walking and running. The following is based on that assumption. The easiest way to do this is by checking how much time has passed since the last time we hit a certain key to run. You'll probably want to keep a KeyCode type variable, which makes it easy to change which key you want for walk/run, but if you're hard coding a key that works as well. Whenever the key is tapped, you do a check of the "current time minus last time" sort. If this time frame is smaller than X, the key has been double-tapped. Regardless, also set last time to current time.

Something like the following:

 // var walkKey: KeyCode = KeyCode.D;
 // var lastTime: float = -1.0f;
 if (Input.GetKeyDown(walkKey))
 {
     if (Time.time - lastTime &lt 0.2f)
     {
         lastTime = Time.time;
         // turn on running
     } else
     {
         lastTime = Time.time;
         // turn on (or switch to) walking
     }
 }
 if (!Input.GetAxis("Horizontal"))
 {
     /* the key is no longer being held */
     // make the character stop moving
 }



Something roughly like that would do the trick. How you actually set the walking/running is up to you. For added control or different playstyles you can mess around with a particular input axis' Gravity and Sensitivity fields. Check out Unity's Input Manager for more on that.

Comment
Add comment · Show 3 · 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 Xatoku · Apr 09, 2011 at 04:23 PM 0
Share

Alright I updated the first post with my new code but it doesn't seem to be working. If I double tap the walkspeed doesn't increase at all, and the lastTime var doesn't reset after I let go.

avatar image Xatoku · Apr 09, 2011 at 04:30 PM 0
Share

Never$$anonymous$$d, I got it working. Thanks a ton!

avatar image kenja0 · Jul 09, 2011 at 06:51 AM 0
Share

I've been running into the same problem and I couldn't figure out how to get around it. I assume it has something to do with Time.time not resetting itself after every press. How did you finally get it to work?

avatar image
1

Answer by Kerihobo · Feb 18, 2015 at 01:01 AM

http://aidtech-game.com/double-tap-button-unity3d/#.VOPjffmUd8E

Comment
Add comment · Show 1 · 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 alexjhones286 · Mar 04, 2017 at 09:09 PM 0
Share

Obrigado'. :) Do Brasil'. :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

[SOLVED] Walk, Run and Sprint with single key 1 Answer

Lerpz ThirdPersonPlayerAnimation bug - running at walking speed 1 Answer

Press Back Button Twice to Quit Game 0 Answers

Different footstep sounds problem! 2 Answers

how can i let my player move converse? 0 Answers


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