Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Razputin · Feb 04, 2015 at 01:33 AM · rigidbodytimerdashcooldown

Free 2d dash ability script!

I have a dashing mechanic in my game. I was using Transform, but since transform doesn't account for physics I was going through walls on occasion. I'm trying to use addforce, but it doesn't have the quick burst of speed effect as previously.

how can I use addforce to make the dash seem more like a dash?

I think my dash's cooldown is messed up too, allowing the player to spam the button, instead of using it once, then having to wait. If someone could check that out it would help me alot too.

         if(Input.GetKey(KeyCode.LeftShift) && canIDash == true){
             if(direction == true)
                 {
                 dashTimer += Time.deltaTime*3;
                 rigidbody.AddForce(Vector3.right*50);
                 
             }
             if (direction == false)
                 {
                 dashTimer += Time.deltaTime*3;
                 rigidbody.AddForce(Vector3.left*50);
                     }
             }
         
             if(dashTimer > .5f)
                 {
                 canIDash = false;
                 dashCooldown = true;
                 }
             if(dashTimer < .5f && dashCooldown == false)
             {
                 canIDash = true;
             }
             if(dashTimer <= 0)
             {
                 dashCooldown = false;
             }
             
         }
 


EDIT:


This is what the script currently looks like, it still doesn't work perfect. If anyone can help it would be awesome.

  public class DashAbility : MonoBehaviour {
 
  public float dashCooldown;
  public bool canIDash = true;
  public Vector2 savedVelocity;
 
  void Update () 
  {
      if(dashCooldown > 0)
      {
          canIDash = false;
          dashCooldown -= Time.deltaTime;
                //if I put saved velocity here, it will continue to move at savedVelocity until dashCooldown = 0? so it can't go here? right?
      }
      if(dashCooldown <= 0)
      {
          canIDash = true;
               //if I put savedVelocity here it doesn't return to savedVelocity until dashCooldown <=0 so... it doesn't go here either right...?
      }
      if(Input.GetKeyDown(KeyCode.LeftShift) && canIDash == true)
      {
          //saves velocity prior to dashing
          savedVelocity = rigidbody.velocity;
          //this part is the actual dash itself
          rigidbody.velocity =  new Vector2(rigidbody.velocity.x*3f, rigidbody.velocity.y);
          //sets up a cooldown so you have to wait to dash again
          dashCooldown = 2;
      }
  }
 

Comment
Add comment · Show 13
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 Alanisaac · Feb 04, 2015 at 01:38 AM 0
Share

F=$$anonymous$$A. Changing force means changing acceleration, so it's not going to be an instantaneous speed change.

Have you tried changing the rigidbody.velocity?

avatar image Razputin · Feb 04, 2015 at 01:42 AM 0
Share

I'll give it a try right now

avatar image Razputin · Feb 04, 2015 at 01:52 AM 0
Share

I dunno, doesn't seem to be really doin it.

         if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftShift) && canIDash == true){
             if(direction == true)
             {
                 dashTimer += Time.deltaTime*3;
                 rigidbody.velocity = new Vector2(25, 0);    
             }
             if (direction == false)
                 {
                 dashTimer += Time.deltaTime*3;
                 rigidbody.velocity = new Vector2(-25, 0);    
                 }
             }


plus the velocity is maintained if you hold left shift and i'm not sure how to change that.

avatar image Razputin · Feb 04, 2015 at 02:27 AM 0
Share

Still lookin for help

avatar image Alanisaac · Feb 04, 2015 at 02:58 AM 0
Share

What do you mean by doesn't seem to be doing it?

What's the original velocity? If it's around 20 to 25, you won't see much difference. I'd expect a dash to be along the lines of rigidbody.velocity = rigidbody.velocity * 2; (or additive, either way).

To stop it from maintaining the velocity, you'll need a way to set the velocity back when you're not holding shift. So when that dash timer runs out, for example, set the velocity back to a standard value.

If you don't have a static velocity in 'normal' (non-dash) mode, you'll want to preserve your previous velocity in a private variable just before you 'dash', then reset it back after.

Does that make sense?


BTW, you have your dash variables set up with dashTimer, dashCooldown, and canIDash. Yet it looks like canIDash is supposed to represent a fixed calculation of information based on the other two.

That situation is perfect for properties. Try this ins$$anonymous$$d:

  private float dashTimer;
  private bool isDashOnCooldown;
  private bool canIDash 
  {
      get { return dashTimer < .5f && isDashOnCooldown == false; }
  }

Then you never have to set canIDash yourself.

Show more comments

2 Replies

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

Answer by Alanisaac · Feb 04, 2015 at 01:48 PM

Ok, I'm pulling this out of the comment thread. From a technical perspective, your dash mechanic may be modeled as a state machine.

We really don't need to get that complicated or formal in our implementation, but it might be helpful to think of dashing as a set of states (Ready, Dashing, Cooldown), with specific conditions that transition from state to state (is the dash key down?, is max dash hit?, is cooldown time over?).

Try out something like the following script. It uses an enum to represent the three states. Notes:

  • Dash can only be activated when you're in the Ready state, and activating it sets the Dashing state. This prevents the dash button continuously attempting to save previous velocity.

  • For the other two cases, I added a bit of value coercion so the timer never goes outside of 0 or the max.

Let me know if you have any questions!

 public class DashAbility : MonoBehaviour {
     
     public DashState dashState;
     public float dashTimer;
     public float maxDash = 20f;
 
     public Vector2 savedVelocity;
     
     void Update () 
     {
         switch (dashState) 
         {
         case DashState.Ready:
             var isDashKeyDown = Input.GetKeyDown (KeyCode.LeftShift);
             if(isDashKeyDown)
             {
                 savedVelocity = rigidbody.velocity;
                 rigidbody.velocity =  new Vector2(rigidbody.velocity.x * 3f, rigidbody.velocity.y);
                 dashState = DashState.Dashing;
             }
             break;
         case DashState.Dashing:
             dashTimer += Time.deltaTime * 3;
             if(dashTimer >= maxDash)
             {
                 dashTimer = maxDash;
                 rigidbody.velocity = savedVelocity;
                 dashState = DashState.Cooldown;
             }
             break;
         case DashState.Cooldown:
             dashTimer -= Time.deltaTime;
             if(dashTimer <= 0)
             {
                 dashTimer = 0;
                 dashState = DashState.Ready;
             }
             break;
         }
     }
 }
 
 public enum DashState 
 {
     Ready,
     Dashing,
     Cooldown
 }

Comment
Add comment · Show 5 · 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 Razputin · Feb 04, 2015 at 04:59 PM 0
Share

I just made a slight change and decided to only save the x velocity ins$$anonymous$$d of the x and y velocity. this way when we return to savedVelocity the character doesn't continue moving up/down for a second(Unless you can think of a better way to stop that). Thank you very much for your help with this it works perfectly and exactly how I wanted it to!

I also changed the questions title so if anyone in the future needs this script they can find it easier :)

avatar image j_masters Razputin · Jan 26, 2017 at 05:20 AM 0
Share

Any chance you can share how you saved just the x velocity?

avatar image meiousei · Jul 23, 2019 at 03:28 AM 0
Share

Hi Alanisaac, I've tried your code with my project but it doesn't seem to do anything with my movement. I've created the needed .cs file and attached to the object (player) with the correct variables like the rigidbody and so on. I see the Dash State, Dash Timer and $$anonymous$$axDash in the inspector in Unity but when I test it out, it detects the button presses, changes to dashing, and when it reaches $$anonymous$$axDash it cools down to 0. Unfortunately it doesn't alter the movement in my character.

I could post my code but it is essentially a copy-paste of what you provided. I would be really glad... like... REALLY GLAD if you help me out. Dash is quite tricky sometimes. I attach a screenshot of what you would expect from my project and the values of the .cs of my PlayerController, as well as the physics of my Rigidbody 2D. alt text

Thanks a lot in advance. I want to progress with my project :)

anotacion-2019-07-22-222300.png (226.2 kB)
avatar image OnlyAskarek meiousei · Mar 24 at 12:40 PM 0
Share

I litlebit corrected code and that code will work try to play with "dashForce" in Unity

 public class ScriptName : MonoBehaviour
 {
     private Rigidbody2D rb;
 
     public DashState dashState;
     public float dashForce;
     public float dashTimer;
     public float maxDash = 20f;
 
     public Vector2 savedVelocity;
 
     private void Start()
     {
         rb = GetComponent<Rigidbody2D>();
     }
 
     void Update()
     {
         switch (dashState)
         {
             case DashState.Ready:
                 var isDashKeyDown = Input.GetKeyDown(KeyCode.LeftShift);
                 if (isDashKeyDown)
                 {
                     savedVelocity = rb.velocity;
                     rb.AddForce(new Vector2(rb.velocity.x * dashForce, rb.velocity.y));
                     dashState = DashState.Dashing;
                 }
                 break;
             case DashState.Dashing:
                 dashTimer += Time.deltaTime * 3;
                 if (dashTimer >= maxDash)
                 {
                     dashTimer = maxDash;
                     rb.velocity = savedVelocity;
                     dashState = DashState.Cooldown;
                 }
                 break;
             case DashState.Cooldown:
                 dashTimer -= Time.deltaTime;
                 if (dashTimer <= 0)
                 {
                     dashTimer = 0;
                     dashState = DashState.Ready;
                 }
                 break;
         }
     }
 }
 
 public enum DashState
 {
     Ready,
     Dashing,
     Cooldown
 }
avatar image unity_143462 · Feb 08, 2020 at 11:19 AM 0
Share

Very useful, helped me make my group project dash ability in school, would recommend to others looking for the same script, only note, makes the player character abruptly stop, without a smooth stopping phase, if there could be one implemented, that would be nice to know.

avatar image
0

Answer by VOTRUBEC · Feb 04, 2015 at 05:20 AM

Give this a shot:

 rigidbody.AddForce(Vector3.right*50,  ForceMode.VelocityChange);

Check Force here at the Docs.

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 Razputin · Feb 04, 2015 at 05:26 AM 0
Share

Not really looking to use AddForce anymore, sorry i'll change the title.

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

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to add a cooldown sort of thing. 1 Answer

C# Weapon overheat Buildup/Cooldown 1 Answer

Cooldown Function Problem 1 Answer

Smooth dashing issues 0 Answers

Unity 3d Elevator Script Not Working Properly 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