Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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 gmarti08 · Dec 02, 2021 at 11:10 PM · dash

Unity 2D -- Dash instantly stops

Hello, I've been working on a simple 2D platformer for a school project! I'm currently making a dash ability for my game, but I've encountered an issue. Whenever I try to dash, it instantly ends barely making me move forward. Can somebody help me fix this issue? I've tried to apply many methods but I couldn't figure it out. Here's the script:

 [SerializeField] private float speed;
 [SerializeField] private float jumpPower;
 [SerializeField] private LayerMask groundLayer;
 [SerializeField] private LayerMask wallLayer;
 private Rigidbody2D body;
 private Animator anim;
 private BoxCollider2D boxCollider;
 private float wallJumpCooldown;
 private float horizontalInput;
 private float waitTime = 2.0f;
 private float timer = 0.0f;
 private float visualTime = 0.0f;
 private bool canDash;

 private void Awake()
 {
     body = GetComponent<Rigidbody2D>();
     anim = GetComponent<Animator>();
     boxCollider = GetComponent<BoxCollider2D>();
 }

 private void Update()
 {
     float horizontalInput = Input.GetAxis("Horizontal");

     if (horizontalInput > 0.01f)
         transform.localScale = Vector3.one;
     else if (horizontalInput < -0.01f)
         transform.localScale = new Vector3(-1, 1, 1);

     anim.SetBool("Running", horizontalInput != 0);
     anim.SetBool("grounded", isGrounded());
     anim.SetBool("onWall", onWall());

     if (wallJumpCooldown > 0.2f)
     {
         body.velocity = new Vector2(horizontalInput * speed, body.velocity.y);

         if (Input.GetKey(KeyCode.Space))
             Jump();
         if (Input.GetKey(KeyCode.C))
             Dash();
     }
     wallJumpCooldown = 3f;

     if (wallJumpCooldown < 0.2f)
     {
         speed = 0;
     }
 }

 private void Jump()
 {
     if (isGrounded())
     {
         body.velocity = new Vector2(body.velocity.x, jumpPower);
         anim.SetTrigger("Jump");
         canDash = true;
     }
     else if (onWall() && !isGrounded())
     {
         body.velocity = new Vector2(-Mathf.Sign(transform.localScale.x) * 16, 14);
         wallJumpCooldown = 0.1f;
     }
 }
 //Here is the Dash, need help here.
 private void Dash()
 {
     if (!isGrounded() && !onWall() && canDash)
     {
         body.velocity = new Vector2(Mathf.Sign(transform.localScale.x) * 20, 0);
         canDash = false;
     }
 }

 private bool isGrounded()
 {
     RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, 0, Vector2.down, 0.1f, groundLayer);
     return raycastHit.collider != null;

 }

 private bool onWall()
 {
     RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, 0, new Vector2(transform.localScale.x, 0), 0.1f, wallLayer);
     return raycastHit.collider != null;
 }
Comment
Add comment · Show 1
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 gmarti08 · Dec 02, 2021 at 11:12 PM 0
Share

I added the canDash = false; so the player can't continue dashing forever, just so you know.

1 Reply

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

Answer by MarekRimal · Dec 03, 2021 at 03:41 PM

I dont understand how your wallJumpCooldown works but my guess is that you are immediately overriding the dash by horizontalInput in the next Update at line 37.

Way to go can be for example store all the velocities to separate variables and then add them all at once to the RigidBody.

Also a tip: Use Time.deltaTime to scale the velocities applying on the rigidbody since then it will works the same on differently fast computers.

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

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

Can Dash only once 2D 2 Answers

Character Controller based 'dash' function moves strangely in one direction, despite values seemingly being correct. 0 Answers

Why am I only allowed to trigger my dash script once? 2 Answers

dash left and right 1 Answer

Help with my Dash move? 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