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)

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.

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 :slight_smile:

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

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:

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:

Welcome to the Community and Good Luck!

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)

}

}

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