• 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 Obi02_ · Feb 16, 2019 at 07:49 AM · script.unity 2drigidbody2dphysics2ddash

How to give player a "dash" ability?

I'm making a 2D isometric/top down kind of game.
My character has this movement script attached:

public class PlayerController : MonoBehaviour {

 public float moveSpeed = 5f;
 public Vector2 lastMove;

 private Rigidbody2D myRigidbody;

 public bool canMoove; //used by scripts to halt player movement

 void Start () {
     myRigidbody = GetComponent <Rigidbody2D> ();
     canMoove = true;
 }
 
 void FixedUpdate () {

     if (canMoove == true) {
         if (Input.GetAxisRaw ("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f) {

             myRigidbody.velocity = new Vector2 (Input.GetAxisRaw ("Horizontal") * moveSpeed, myRigidbody.velocity.y);
             playerMoving = true;
             lastMove = new Vector2 (Input.GetAxisRaw ("Horizontal"), 0f);
         } //movement on the horizontal axis

         if (Input.GetAxisRaw ("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f) {

             myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, Input.GetAxisRaw ("Vertical") * moveSpeed);
             playerMoving = true;
             lastMove = new Vector2 (0f, Input.GetAxisRaw ("Vertical"));
         } //movement on the vertical axis

         if(Input.GetAxisRaw ("Horizontal") < 0.5f && (Input.GetAxisRaw ("Horizontal") > -0.5f))
         {
             myRigidbody.velocity = new Vector2 (0f, myRigidbody.velocity.y);
         } //stops ice movement effect

         if (Input.GetAxisRaw ("Vertical") < 0.5f && Input.GetAxisRaw("Vertical") > -0.5f)
         {
             myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, 0f);
         } //stops ice movement effect
     } else {
         myRigidbody.velocity = new Vector2 (0f, 0f);
     }
 }  

I would like to give the player a "dash" ability similar to those in Hollow Knight and Hyper Light Drifter. Where while moving, the character dashes forward in the direction they're moving (including along the diagonal). Or if they aren't moving, they just dash in the direction that they're facing.

Any ideas about how I could go about this? I've tried several scripts but none of them allow for a diagonal dash - only strictly horizontal and vertical. Any help would be appreciated.

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

0 Replies

· Add your reply
  • Sort: 

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

113 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

Related Questions

How to boxcast where a dynamic rigidbody2D's box collider will be in the next frame? 1 Answer

In unity 2D c# how to rotate an object like geometry dash? 1 Answer

How can you make gravity to not have any acceleration but with const velocity? 1 Answer

Why does poligon collider 2d restricts movement of box collider? 0 Answers

Need Help with MousePos and dashing 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