• 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 /
  • Help Room /
avatar image
Question by Cubemation · Feb 24, 2018 at 04:53 PM · 3d64bit5.0.1p4

How can i make the player dash forward ?

Hello. I have a problem, and i'm a bit stuck.

I am using this code for dashing...

         `if (Input.GetKeyDown(KeyCode.B))
         {
             currentDashTime = 0.0f;
         }
         if (currentDashTime < maxDashTime)
         {
             moveDir = new Vector3(0, 0, dashSpeed);
             currentDashTime += dashStoppingSpeed;
         }
         else
         {
             Debug.Log ("Nope");
         }
         controller.Move(moveDir*Time.deltaTime);`

..but i keep dashing only on the Z axis. Any way to fix this ? Am i doing something wrong ? Any advice is welcome !

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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Alanisaac · Feb 24, 2018 at 08:53 PM

Can't answer this fully without knowing what controller.Move() does, but my hunch would be that you're only moving on the Z-axis because you're passing that function a vector along the Z-axis. What you might try doing instead is using a vector to represent your direction, then normalizing it and multiplying it by dash speed:

          if (Input.GetKeyDown(KeyCode.B))
          {
              currentDashTime = 0.0f;
          }
          if (currentDashTime < maxDashTime)
          {
              // replace me with the direction you want to dash in
              // to dash forward, this should be a vector in the direction the player is looking
              // if you have access to your player in a variable, this would be something like:
              // var directionYouWantToDash = player.transform.forward;
              var directionYouWantToDash = new Vector3(0, 0, 0); 

              // this will make your velocity exactly equal to dash speed, regardless of direction
              moveDir = directionYouWantToDash.normalized * dashSpeed;

              currentDashTime += dashStoppingSpeed;
          }
          else
          {
              Debug.Log ("Nope");
          }
          controller.Move(moveDir*Time.deltaTime);`

Comment

People who like this

0 Show 1 · 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 Cubemation · Feb 25, 2018 at 01:22 PM 0
Share

Thanks for the answer :) By the way controller reference is a reference to the CharacterController

avatar image

Answer by BraydenB · Apr 05, 2020 at 03:31 AM

Here's my not so clean code but it works @Cubemation

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
     public float speed = 10f;
     public float dashLength = 0.15f;
     public float dashSpeed = 100f;
     public float dashResetTime = 1f;
 
     public CharacterController characterController;
 
     private Vector3 dashMove;
     private float dashing = 0f;
     private float dashingTime = 0f;
     private bool canDash = true;
     private bool dashingNow = false;
     private bool dashReset = true;
 
     void Update()
     {
         float moveX = Input.GetAxis(“Horizontal”);
         float moveZ = Input.GetAxis(“Vertical”);
 
         Vector3 move = transform.right * moveX + transform.forward * moveZ;
 
         if (move.magnitude > 1)
         {
             move = move.normalized;
         }
 
 
         if (Input.GetButtonDown("Dash") == true && dashing < dashLength && dashingTime < dashResetTime && dashReset == true && canDash == true)
         {
             dashMove = move;
             canDash = false;
             dashReset = false;
             dashingNow = true;
         }
 
         if (dashingNow == true && dashing < dashLength)
         {
             characterController.Move(dashMove * dashSpeed * Time.deltaTime);
             dashing += Time.deltaTime;
         }
 
         if (dashing >= dashLength)
         {
             dashingNow = false;
         }
 
         if (dashingNow == false)
         {
             characterController.Move(move * speed * Time.deltaTime);
         }
 
         if (dashReset == false)
         {
             dashingTime += Time.deltaTime;
         }
 
         if (characterController.isGrounded && canDash == false && dashing >= dashLength)
         {
             canDash = true;
             dashing = 0f;
         }
 
         if (dashingTime >= dashResetTime && dashReset == false)
         {
             dashReset = true;
             dashingTime = 0f;
         }
     }
 }
Comment

People who like this

0 Show 0 · 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.

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

84 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

Related Questions

Can't get raycast to mousePosition 0 Answers

I'm seeing and walking through walls 2 Answers

Using a directional vector to orient a sphere? 0 Answers

Top down 3D shooter - mouse aim 0 Answers

How could I approach a melee ability system 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