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 markenzoshukshuk · Dec 31, 2021 at 06:42 PM · movementvector3charactercontrollercharacter movementinput.getaxis

Dashing stops when i let go of WASD keys

Hello, I have a dashing script that allows you to dash based on your input (I.e. W + D is forward right). Now, as i stated in my question, The dashing seems to stop whenever I let go of wasd.

Here are the codes that are important to the dash:

 private void Update()
     {
         float horizontal = Input.GetAxisRaw("Horizontal");
         float vertical = Input.GetAxisRaw("Vertical");
         Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
         float targetangle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
         target = Quaternion.Euler(0f, targetangle, 0f);
 
         if (Input.GetKey(KeyCode.Q) && candash)
         {
             StartCoroutine(Dash(dashtime, dashcd));
         }
 
         if (isdashing)
         {
             dashspeed = Mathf.Lerp(dashspeed, enddashspd, dashrate);
 
             cc.Move(_dashDirection * dashspeed * Time.deltaTime);
         }
     }  
 
 IEnumerator Dash(float s, float cd)
     {
         isdashing = true;
         candash = false;
         _dashDirection = target * Vector3.forward;
 
         yield return new WaitForSeconds(s);
 
         isdashing = false;
         dashspeed = initialdashspeed;
 
         yield return new WaitForSeconds(cd - s);
 
         candash = true;
     }


The thing i used for the player is a charactercontroller (as shown by the "cc" in the script). Im aware that the problem is that im using input.getaxis but my main problem is how can i fix/change the code to make it so that, Whenever I press the dash button, I still dash in the desired direction i wanted to go in even if i let go of the WASD keys.

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 xxmariofer · Dec 31, 2021 at 08:31 PM 0
Share

Are you using any other extra code to move the character? I can't see any issue with your code

avatar image markenzoshukshuk xxmariofer · Jan 01 at 06:25 AM 0
Share

Well, yes i do have extra code that i use to move my character, I didn't add them in the code i presented because I thought it was irrelevant.

avatar image xxmariofer markenzoshukshuk · Jan 01 at 11:42 AM 0
Share

Could you add it?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by pieandseal · Jan 01 at 11:52 PM

Store direction right before the function is called and then use the stored direction instead of the current direction

  float horizontal = Input.GetAxisRaw("Horizontal");
  float vertical = Input.GetAxisRaw("Vertical");
  Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
 
 
 void DashExample()
 {
        Vector3 startDir = direction;
 
        // dash code (move towards startDir, not direction)
 }


hope this helped

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 xxmariofer · Jan 02 at 09:31 AM 0
Share

He is already storing the direction in a variable, this won't fix his issue

avatar image rage_co xxmariofer · Jan 02 at 12:34 PM 0
Share

but he overwrites the variable every frame, so clearly it's not going to be carried over, although i don't see how @pieandseal 's solution will help

avatar image xxmariofer rage_co · Jan 02 at 01:11 PM 0
Share

No, he is not overriding the dash direction everyframe

Show more comments
avatar image markenzoshukshuk · Jan 02 at 11:14 PM 0
Share

Sorry, I already found a solution to my problem. Apparently all i had to do was disable the input/changing of Vector3 direction via using bools. So now the Vector3 direction part code looks a bit something like this:

  if (cangetdir)
         {
             float horizontal = Input.GetAxisRaw("Horizontal");
             float vertical = Input.GetAxisRaw("Vertical");
             Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
             float targetangle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
             target = Quaternion.Euler(0f, targetangle, 0f);
         }

When isdashing is false, I can get the dash direction i need and when i press the dash button, it sets cangetdir to false and apparently that freezes my input to the direction i was pressing before the dash.

Thank you tho

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

224 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 keep velocity and add onto it from any rotation. 1 Answer

Player moves diagonally even when not telling him to 0 Answers

Using Input.GetAxis on a 2 Player Game 2 Answers

Character Controller Movement - Different speeds on different axis 1 Answer

Character goes through walls 3 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