• 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 Drexx · Nov 05, 2014 at 09:55 PM · c#topdowndash

How do I script the player character to dash?

Hello, I'm developing a prototype for my 3D topdown game in C#. I have a capsule as my player character; he can move (although twice as fast when moving diagonally), rotates according to what direction he is moving, and has a camera following him without rotating with him.

Now, I'm trying to get him to 'dash'--to move a few metres in the direction he's facing in half a second when the 'Z' key is pressed.

So far, I made two floats: - public float dashDistance & public float dashSpeed

Where do I go from here? (I'm very new to programming)

Comment
Add comment
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

5 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Kerihobo · Nov 05, 2014 at 10:42 PM

if you have a characterController component, you can use

 controller.move(moveDirection * Time.deltaTime);

where moveDirection is a variable you declared storing a vector3. (X, Y, Z) <--- 3 vectors.

So something along the lines of

 public vector3 moveDirection;
 public float maxDashTime = 1.0f;
 public float dashSpeed = 1.0f;
 public float dashStoppingSpeed = 0.1f;
 
 private float currentDashTime;
 
 void Start()
 {
     currentDashTime = MaxDashTime;
 }
 
 void Update()
 {
     if (input.getButtonDown(KeyCode.Z))
     {
         currentDashTime = 0.0f;
     }
     if (currentDashTime < MaxDashTime)
     {
         moveDirection = new vector3(0, 0, dashSpeed);
         currentDashTime += dashStoppingSpeed;
     }
     else
     {
         moveDirection = vector3.zero;
     }
     controller.move(moveDirection*Time.deltaTime);
 }
 

the dashStoppingSpeed will increase every frame, and when it reaches 1.0f, your dash will stop. so decrease that if you want your dash to last longer.

this should work, but I dont know how this will conflict with the rest of your character movement controls, adjust it to suit your needs :)

I should warn you that I'm not at home so was not able to test this.

Comment
Add comment · 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 KhazHusky · Apr 26, 2018 at 01:47 PM 0
Share

Hello, Sorry to necro this thread, I tested out your code. The issue I came to was that it will always move up the "z" axis which I guessing comes form the: "moveDirection = new Vector3(0, 0, dashSpeed);" Do you have a suggestion to have it move the direction the player is currently moving?

avatar image
1

Answer by Serinx · Nov 05, 2014 at 10:55 PM

This really depends on how you are moving your player. Are you using physics or transform.translate?

I assume you already have the direction the player is facing(Transform.forward?) which is being used for your movement.

Now you just need to Lerp the current position to the direction * dashDistance and I guess dashSpeed would need to be used to determine how quickly the lerp happens.

Here are some references that should help you understand these functions: http://docs.unity3d.com/ScriptReference/Vector3.Lerp.html http://docs.unity3d.com/ScriptReference/Transform-forward.html

The Lerp example code should give you something to base your dash code on.

To solve your "Moving faster in diagonal" problem, you may need to use Vector3.Normalize on your direction vector to always return the same magnitute: http://docs.unity3d.com/ScriptReference/Vector3.Normalize.html

Welcome to the Community and Good Luck!

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
avatar image
0

Answer by Frackle · Oct 05, 2016 at 08:01 PM

If you want a simple 2D dash, I'd recommend you using this one i made:

using UnityEngine; using System.Collections;

public class Dash : MonoBehaviour { public float speed = 1f;

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
             if (Input.GetKey(KeyCode.D))
                     transform.position += new Vector3 (speed * Time.dektaTime, 0.1f, 0.0f)
 
 }

}

Comment
Add comment · Show 2 · 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 ZenTheDev1 · Mar 16, 2020 at 05:02 PM 0
Share

Fixed version: void Update () { if (Input.GetKey(KeyCode.D)) transform.position += new Vector3 (speed * Time.deltaTime, 0.1f, 0.0f) }

avatar image ZenTheDev1 · Mar 16, 2020 at 05:05 PM 0
Share

and you forgot a semicolon

avatar image
2

Answer by rogersh124 · Aug 21, 2018 at 10:51 PM

based off of @Kerihobo script you can make one small change to make it go forward everytime.

 public class PlayerDash : MonoBehaviour {
 
    public Vector3 moveDirection;
 
     public const float maxDashTime = 1.0f;
     public float dashDistance = 10;
     public float dashStoppingSpeed = 0.1f;
     float currentDashTime = maxDashTime;
     float dashSpeed = 6;
     CharacterController controller;
 
     
 
     private void Awake()
     {
         controller = GetComponent<CharacterController>();
     }
 
     // Update is called once per frame
     void Update () {
         if (Input.GetButtonDown("Fire2")) //Right mouse button
         {
             currentDashTime = 0;                
         }
         if(currentDashTime < maxDashTime)
         {
             moveDirection = transform.forward * dashDistance;
             currentDashTime += dashStoppingSpeed;
         }
         else
         {
             moveDirection = Vector3.zero;
         }
         controller.Move(moveDirection * Time.deltaTime * dashSpeed);
     }
 }


Don't worry as this shouldn't interfere with over controller movement until the player uses the dash action.

Comment
Add comment · 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 Kerihobo · Aug 22, 2018 at 08:46 AM 0
Share

I take back what i said there though. I'd use a coroutine nowadays to detect the double-tap.

avatar image
0

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

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

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

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

13 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

Related Questions

How can I dash in a specific direction 1 Answer

2D Dash attack 0 Answers

Unity2d Top Down: Can't move character with Animator enabled 0 Answers

How to get acceleration in a top-down angle 0 Answers

Add an animation clip with AnimationClip using cSharp script 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