- Home /
while Loop isn't running, or not doing what I expect it to.
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;
}
}
}
Answer by EHogger · Jul 18, 2013 at 09:03 AM
Your while condition is false when it is first called, because dashStep
is exactly equal to Time.time
. Then canDash
is set to false, so the while loop is not called again.
I wonder if While(Time.time-dashStep >= 1.0F)
is actually correct? This would only execute after one second has passed. It seems like you actually want it to execute straight away and stop after some time (1 second in this case), which would be: While(Time.time-dashStep <= 1.0F)
Hrm. I was hoping it would be that simple as I hadn't tried that before, just tried running it like that and it freezes Unity, which makes me think that the while loop is repeating forever. =\ So ins$$anonymous$$d of always being false now, it's always true. Hrm.
Answer by gjf · Jul 18, 2013 at 09:03 AM
pure guess, but it seems like while(Time.time-dashStep >= 1.0F)
executes immediately and Time.time
hasn't incremented yet, so Time.time = dashStep
therefore your while
loop will not execute and the code continues to canDash = false;
g.
This seems to be the case. Swapping the greater than to a less than just makes it always be true and loop infinitely. =\ What would be the proper way of checking to see if 1 second has passed then, and looping and action during that 1 second?
Your answer
