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