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)
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.
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?
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:
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)
}
}
Fixed version: void Update () { if (Input.GetKey(KeyCode.D)) transform.position += new Vector3 (speed * Time.deltaTime, 0.1f, 0.0f) }
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?
– KhazHusky