Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 harpoaian · Jul 20, 2017 at 05:02 PM · 2d game2d-platformerdash

Can Dash only once 2D

Hello everyone I have implemented a dash mechanic in my 2D side scrolling project and I got it to work. The thing is it only works once. I think it is because the isDashing variable isn't going back to false. I am just having a hard time understanding why. If anyone could help me that would be amazing! Have a great day!

Kindly,

Harpoaian

here is my dash function void Dash() {

         if (onGround == true && Input.GetKey(KeyCode.F) && isDashing == false  && thePlayer.DashTimer() <= 1.0f)
         {
             float dashDirection = Input.GetAxis("Horizontal");

             float dashSpeed = 500;

             thePlayer.UpdateDashTimer();

             GetComponent<Rigidbody2D>().velocity = new Vector2(thePlayer.MaxDashSpeed() * dashDirection, GetComponent<Rigidbody2D>().velocity.y);
             isDashing = true;
             Debug.Log("I am dashing!");
             Debug.Log("DashTime = " + thePlayer.DashTimer());
             Debug.Log(isDashing);
             rb.AddForce(transform.right * dashSpeed);


         }

         else if(isDashing == true)
         {
             isDashing = false;
             Debug.Log(isDashing);
         }

    

     
 }
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
0
Best Answer

Answer by MattG54321 · Jul 21, 2017 at 01:44 PM

Without seeing the rest of your code, I'm not exactly sure why your method isn't working. However, here's a potentially simpler way to do it:

 public float dashCooldown = 1f; //How long the player has to wait before they can dash again
 
 private float lastDash = 0f; //Amount of time since we last dashed
 
 void Update () {
     checkDash();
 }
 
 private void checkDash() {
     lastDash += Time.deltaTime;
     //If we've waited long enough and the player presses the dash key
     if (lastDash >= dashCooldown && Input.GetKeyDown(KeyCode.F)) {
         lastDash = 0f;
         //I've pasted some of your dash code here:
         float dashDirection = Input.GetAxis("Horizontal");
         float dashSpeed = 500;
         GetComponent<Rigidbody2D>().velocity = new Vector2(thePlayer.MaxDashSpeed() * dashDirection, GetComponent<Rigidbody2D>().velocity.y);
         rb.AddForce(transform.right * dashSpeed);
     }
 }

I've done away with isDashing, because we can just check the timer to see if we can dash again. Also, I recommend making dashSpeed public and outside of any methods, so you can easily change it in the inspector.

I'm at work, so this is untested code. Feel free to come back with any questions. Good luck!

Comment
Add comment · Show 4 · 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 harpoaian · Jul 21, 2017 at 02:16 PM 0
Share

Thank you! Now the only thing I need is to make it stop after one second. I want this dash to function like in the $$anonymous$$ega $$anonymous$$an X series. At the current moment my player controller just keeps going when I am holding the F key. I want the player to dash for 1 second and then the timer reset back to zero. I don't want the player to be able to hold the dash forever XD. How should I approach that? Also thank you again good sir!

avatar image harpoaian · Jul 21, 2017 at 02:26 PM 0
Share

Alright I figured it out! I just had to change the Input.Get$$anonymous$$ey to Input.Get$$anonymous$$eyDown. Thank you very much :) You have helped me out a lot.

avatar image MattG54321 harpoaian · Jul 21, 2017 at 03:58 PM 0
Share

Always happy to help. :)

avatar image crossproductions021 MattG54321 · May 08, 2020 at 05:40 PM 0
Share

I tried this code but for some reason $$anonymous$$axDashSpeed keeps giving me a compiler error.

Assets\Scripts\Dash2.cs(30,74): error CS1061: 'object' does not contain a definition for '$$anonymous$$axDashSpeed' and no accessible extension method '$$anonymous$$axDashSpeed' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

Can you please help? I'm new to coding and I am still learning.

avatar image
0

Answer by theonciest · Jan 19, 2018 at 03:12 AM

@MattG54321 Hi there, is there any way to convert this to 3d?

https://pastebin.com/LWSFVLrH

I feel it is this portion here -

                 // detect input movement
                 var moveHorizontal = Input.GetAxis("Horizontal");
                 var moveVertical = Input.GetAxis("Vertical");
                 IsMoving = moveHorizontal != 0 || moveVertical != 0;
  
                 IsRunning = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
                 if(IsRunning)
                 {
                     speed = runSpeed;
                 }

If there is any way you can help it would be greatly appreciated man. Been at this for days now. I've changed a few 2D scripts, and one really helped me understand it a bit more, but actually implementing it is impossible with my knowledge.

https://youtu.be/6WdIt3_EvYs

Thanks for you time

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

77 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 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 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

I need help with my dash 0 Answers

Unity 2D platformer collider 1 Answer

I need to shoot a projectile in my 2D platformer in the opposite direction my player is dashing which can be in any direction the player points the joystick 2 Answers

Move Player forward then back after seconds 1 Answer

Way to achieve 3 lane mechanism in a 2D game 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