• 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
Question by JumpingCholla · Apr 27, 2015 at 12:25 AM · animation2dinputcoroutineinfinite

How to prevent holding down a key to infinitely loop an action?

Hello,

I am having trouble making my 2d sprite perform a "Dash" Animation when I press down the space key. When the space key is pressed down, my character does begin to dash, and the animation does trigger, but the only problem is is that I can hold down the space key and the "Dash" infinitely loops. I only want the character to dash for about a second per press of the space key. I tried making a Coroutine for the desired result, but I must've done it wrong.

Here's the code:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
 
     Animator anim;
 
     public float walkSpeed = 1f;
     public float dashSpeed = 3f;
 
 
     void Start () 
     {
         anim = GetComponent<Animator> ();
     }
 
     void Update () 
     {
         float input_x = Input.GetAxisRaw ("Horizontal");
         float input_y = Input.GetAxisRaw ("Vertical");
         float input_dash = Input.GetAxis ("Jump");
 
         bool isWalking = (Mathf.Abs (input_x) + Mathf.Abs (input_y)) > 0;
         bool isDashing = (Mathf.Abs (input_dash) > 0);
 
         anim.SetBool ("isWalking", isWalking);
         anim.SetBool ("isDashing", isDashing);
 
 
         if(isWalking && isDashing == false)
         {
             anim.SetFloat("x", input_x);
             anim.SetFloat("y", input_y);
 
             transform.position += new Vector3(input_x, input_y, 0).normalized * walkSpeed * Time.deltaTime;
         }
 
         if(isDashing)
         {
             isWalking = false;
 
             anim.SetFloat("x", input_x);
             anim.SetFloat("y", input_y);
             transform.position += new Vector3(input_x, input_y, 0).normalized * dashSpeed * Time.deltaTime;
 
             StartCoroutine(Dash());
             isDashing = false;
         }
     }
 
     IEnumerator Dash()
     {
         yield return new WaitForSeconds(5);
     }
 
 }

Thanks for any help, I am a beginner at best, and simple things like this still confuse me.

Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by acorrow · Apr 27, 2015 at 12:39 AM

Instead of mapping to the "Jump" axis, just use a normal onClick event on a button (assuming you are using something like a gui canvas..) if you are using raw input it's a little different to grab a specific button, but anyway you slice it, you likely don't really want to assign the transform a new position to dash (or really to walk/move at all, but that's a different topic touched on later here).

However, try just using AddForce to the rigidbody to dash. So basically, just keep it how you have it, but take out the dash speed in the transform.position movement. Instead build another method that is your "dash" method, that when a butotn is clicked, you use a quick AddForce to your already existing velocity, set the bool for your animation to true, fire your enumerator, and after it's yield, set the bool for your animatin to false. ALl of this effectively "pushing" him harder/faster forward... OR, what I would likely do is to just have a check in the update loop to see if your Velocity is HIGHER than your natural "Limit" (basically meaning you are "dashing". FOr example if you are moving your transform a X rate, once you go OVER (or under the negative value if you can move left and right) then you are "Dashing" and the bool for animation should be true.

Also, depending on your game, you might want to just try using AddForce for all your movement, and multiplying it based on what is being input. For example, if you are using a Joystick, and the X axis will be between -1 and 1, then create a "Max Speed" and use AddForce to apply your axis value * your multiplier. Then, if you are pushing your "dash" button, just make that multiplier double or whatever you want.

One last thing, you can save yourself a LOT of trouble by using a Blend Tree to manage the transition from walking to dashing....

http://docs.unity3d.com/Manual/class-BlendTree.html

Just pass in a "speed", set the threshold for each animation, and let it do the dirty work....

Hope that Helps!

Adam

Comment
JumpingCholla

People who like this

1 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 JumpingCholla · Apr 27, 2015 at 12:43 AM 0
Share

Thank you for the information.

I'm going to get on this now, I'm a novice at programming, so it will be difficult. Regardless, the information is very much appreciated.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

2D Animation does not start 1 Answer

How to make realistic 2D balloon physics 0 Answers

Problem With Coroutine - Infinite Loop? 1 Answer

GetButton will only play animation when held down 2 Answers

Do Something Relative to Animation Time 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