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 !

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);`

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;
        }
    }
}