FPS Dashing Issue, Need to smooth and bool issue.
I have started to create a dashing system so the player can dash left, right, and backwards. I have it so when the user double taps the A,S, and D button they will move over. Is there anyway to smooth the movement? I am also having an issue with the "isDashing" bool, it doesn't seem to be updating for some reason. Here is my code using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Dashing : MonoBehaviour {
public float tapSpeed = 0.5f; //in seconds
public float dashSpeed = 50;
public float waitTime = 5f; //in seconds
public bool isDashing;
private float lastTapTime = 0;
// Use this for initialization
void Start()
{
lastTapTime = 0;
isDashing = false;
StartCoroutine(WaitTime());
}
// Wait time after dashing
public IEnumerator WaitTime()
{
yield return new WaitForSeconds(waitTime);
isDashing = false;
}
// Update is called once per frame
void Update()
{
if (isDashing == false)
{
if (Input.GetKeyDown(KeyCode.A))
{
if ((Time.time - lastTapTime) < tapSpeed) //double tap
{
isDashing = true;
transform.Translate(Vector3.left * dashSpeed * Time.deltaTime);
Debug.Log("Dash left");
}
lastTapTime = Time.time;
}
if (Input.GetKeyDown(KeyCode.D))
{
if ((Time.time - lastTapTime) < tapSpeed) //double tap
{
isDashing = true;
transform.Translate(Vector3.right * dashSpeed * Time.deltaTime);
Debug.Log("Dash right");
}
lastTapTime = Time.time;
}
if (Input.GetKeyDown(KeyCode.S))
{
if ((Time.time - lastTapTime) < tapSpeed) //double tap
{
isDashing = true;
transform.Translate(Vector3.back * dashSpeed * Time.deltaTime);
Debug.Log("Dash back");
}
lastTapTime = Time.time;
}
}
if(isDashing == true)
{
WaitTime();
}
}
}
Answer by jasonlee88870 · Jan 24, 2018 at 06:22 PM
@GreenHornStudios You did not set a if statement for isDashing = false.
Add it in to your Input.GetKeyDown if statement E.g
if (Input.GetKeyDown(KeyCode.S) && isDashing == false) { ............. }
Also you might want to try updating the isDashing once the dash is over.
Write this after lastTapTime = Time.time and Debug.Log("Dash back");
isDashing = false;
hope this helps :D
Answer by Hamburgert · Jan 24, 2018 at 07:30 PM
I'm assuming that by "smooth" you mean acceleration instead of an instant fullspeed movement. You will need to track more variables for that to work. I'm sure there are many ways to do this, but I will outline a method I'm using
Here is some almost real code, in super simplified form. Perhaps enough to give an idea of what it will take:
float dashSpeed = 50F;
float maxAcceleration = 50F; // how much speed can change in 1 second
Vector3 currentSpeed = Vector3.zero;
Vector3 targetSpeed = Vector3.zero;
// On input, set desired speed
targetSpeed.x = -dashSpeed; // left input
targetSpeed.x = dashSpeed; // right input
targetSpeed.z = -dashSpeed; // backwards input
// On update, move the current speed towards the target speed before actually moving
currentSpeed = Vector3.MoveTowards(currentSpeed, targetSpeed, maxAcceleration * Time.deltaTime);
transform.Translate(currentSpeed);
Answer by SageGlaze · Feb 25, 2019 at 01:05 PM
I'm not certain how you could go about smoothing the movement, but as for the issue with your isDashing bool simply remove your StartCoroutine method from Start() and instead call it at the end of each of your double tap if statements. Something like this
if ((Time.time - lastTapTime) < tapSpeed) //double tap
{
isDashing = true;
transform.Translate(Vector3.right * dashSpeed * Time.deltaTime);
Debug.Log("Dash right");
StartCoroutine(WaitTime());
}
I'd probably also remove if(isDashing == true) { WaitTime(); } Hope this helps!