- Home /
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.
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
Thank you for the information.
I'm going to get on this now, I'm a novice at program$$anonymous$$g, so it will be difficult. Regardless, the information is very much appreciated.