I’ve been wracking my head over this for several hours, I can’t figure out why this won’t work. No matter what I seem to try, this why loop will only go through once. I’m using a platformer-style movement system in just X and Y and I want to add a “dash” functionality so that the player can perform a quick burst forward or backward. If you’ve played the game Rogue Legacy, the dash in that game is exactly what I’m trying to replicate.
Here’s the problem code:
void Dash(int LeftRight){
if(canDash){
float dashStep = Time.time; //record the current time
moveDirection.x = 10*LeftRight; //LeftRight is either -1 or 1, -1 for left movement 1 for right movement
while(Time.time-dashStep >= 1.0F){
Debug.Log("Moved"); //this never fires?
controller.Move(moveDirection*Time.deltaTime);
}
canDash = false;
dashCooldownTimer = Time.time;
}
Here’s the full code:
using UnityEngine;
using System.Collections;
public class Movement2d : MonoBehaviour {
public float speed = 3.0F;
public float jumpSpeed = 6.0F;
public float playerGravity = 20.0F;
public float doubleJumpPowerDamp = 1.0F;
public float dashCooldown = 5.0F;
private Vector3 moveDirection = Vector3.zero;
private bool doubleJump = false;
private bool canDash = true;
private float dashCooldownTimer;
private CharacterController controller;
//private int jumpTimer = 0;
void Start () {
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
if(controller.isGrounded) {
//Left/Right Movement
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
//Jump
if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)){
moveDirection.y = jumpSpeed;
}
if(moveDirection.y == 0){
doubleJump = true;
}
//Dash
if(Input.GetKeyDown(KeyCode.E)){
Dash (1);
}
if(Input.GetKeyDown(KeyCode.Q)){
Dash (-1);
}
//Dash Cooldown Reset
if(!canDash){
if(Time.time - dashCooldownTimer >= dashCooldown){
canDash = true;
}
}
}
//Allow movement back and forth mid-air & double jumping
else if(!controller.isGrounded && Mathf.Abs(moveDirection.x)<jumpSpeed) {
moveDirection.x = Input.GetAxis("Horizontal") * (jumpSpeed*0.8F); //Can move left or right in the air at 80% speed
//Double Jump
if((Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow)) && doubleJump==true && !controller.isGrounded){
moveDirection.y = jumpSpeed-doubleJumpPowerDamp; //Double Jump "strength". 0 makes double jump same as first jump
doubleJump = false;
}
//Dash
if(Input.GetKeyDown(KeyCode.E) && !controller.isGrounded){
Dash (1);
}
if(Input.GetKeyDown(KeyCode.Q) && !controller.isGrounded){
Dash (-1);
}
}
//Rotate left or right based on movement
if(moveDirection.x > 0)
transform.localScale = new Vector3(1, 1, 1);
else if(moveDirection.x < 0)
transform.localScale = new Vector3(-1, 1, 1);
moveDirection.z = 0; //Lock the Z position of the player.
moveDirection.y -= playerGravity * Time.deltaTime; //Apply gravity to the player
controller.Move(moveDirection * Time.deltaTime); //Move the player
}
void Dash(int LeftRight){
if(canDash){
float dashStep = Time.time;
moveDirection.x = 10*LeftRight;
while(Time.time-dashStep >= 1.0F){
Debug.Log("Moved");
controller.Move(moveDirection*Time.deltaTime);
}
canDash = false;
dashCooldownTimer = Time.time;
}
}
}