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 FruitCakeUnicorn · Feb 19, 2017 at 10:57 PM · c#velocityvector2

2D top down dash code

'Ive been looking around to get a good dash code for my character and i cant get my velocity to change hoping you could help here's my code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DashAbility : MonoBehaviour {
 
     public float dashCooldown;
     public bool canIDash = true;
     public Vector2 CurrentVel;
     // Use this for initialization
     void Start () {
        
 
     }
     
     // Update is called once per frame
     void Update () {
 
         CurrentVel = GetComponent<Rigidbody2D>().velocity;
 
         if (dashCooldown > 0)
         {
             canIDash = false;
             dashCooldown -= Time.deltaTime;
         }
 
         if (dashCooldown <= 0)
         {
             canIDash = true;
         }
 
         if (Input.GetKeyDown(KeyCode.LeftShift) && canIDash == true)
         {
             
             //this part is the actual dash itself
             CurrentVel = new Vector2(CurrentVel.x * 10, CurrentVel.y * 10);
             //sets up a cooldown so you have to wait to dash again
             dashCooldown = 2;
         }
     }
 }
 
Comment
Add comment · Show 3
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 Kossuranta · Feb 20, 2017 at 08:29 AM 1
Share

Need to see whole code to help you, like how are you moving player?

Also please rename your CurrentVel as currentVel. $$anonymous$$ethods like Update and Start should be started with uppercase letter and varaibles like currentVel and dashCooldown with lowercase letter. It will work either way, but you can already see coloring being wrong (CurrentVel and Update have same color).

If you need canIDash in somewhere else than this then it's just fine, but if not then you don't need it at all. You could just simply check for the time like this:

 void Update ()
 {
     currentVel = GetComponent<Rigidbody2D>().velocity;
     dashCooldown -= Time.deltaTime;
 
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftShift) && dashCooldown <= 0)
     {
         //this part is the actual dash itself
         currentVel = new Vector2(currentVel.x * 10, currentVel.y * 10);
         //sets up a cooldown so you have to wait to dash again
         dashCooldown = 2;
     }
 }

Oh and you should create new variable private Rigidbody2D rb2d; and then get it once in start rb = GetComponent(); as it's somewhat slow thing to do. After that you get velocity with just currentVel = rb2d.velocity;

avatar image FruitCakeUnicorn Kossuranta · Feb 20, 2017 at 08:32 AM 0
Share

Thanks for responding! This is my movement code.

 public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour
     {
         public float walkSpeed = 4f;
         public float runSpeed = 8f;
 
         // Use this for initialization
         void Start()
         {
            
         }
 
         // Update is called once per frame
         void FixedUpdate()
         {
             Vector2 targetVelocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
             GetComponent<Rigidbody2D>().velocity = targetVelocity * walkSpeed;
         }
avatar image ThisGuyThatsHere FruitCakeUnicorn · Feb 20, 2017 at 09:23 AM 1
Share

The problem might be that you dash, but, well, you reset the velocity in movement section so your dash velocity gets overrided.

1 Reply

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

Answer by ThisGuyThatsHere · Feb 20, 2017 at 09:49 AM

The problem is dashing gets overrided by movement, I guess you don't really want to play with curves etc. so let's keep it simple.

Instead of doing modifications in the ability which could be pretty advanced you should be fine with simple player movement rework:

 public class PlayerMovement : MonoBehaviour
      {
          public float walkSpeed = 4f;
          public float runSpeed = 8f;
  
          // Use this for initialization
          void Start()
          {
             
          }
  
          // Update is called once per frame
          void FixedUpdate()
          {
              Vector2 targetVelocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
              if(targetVelocity.magnitude() * walkspeed > GetComponent<Rigidbody2D>.velocity.magnitude()) GetComponent<Rigidbody2D>().velocity = targetVelocity * walkSpeed; //So, dash results in higher velocity than normal movement? then let's override it only if it got slower than movement!
          }
 }

This is far from perfect and you can obviously do that cpu cheaper way, but should work.

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

9 People are following this question.

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

Related Questions

Can anyone tell me why this code isn't working? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

HOLD THE JUMP BUTTON TO JUMP HIGHER 0 Answers

Transferring velocity from kinematic to a rigid body 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