Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jurassic · Jul 18, 2013 at 08:15 AM · movementplatformer

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;
         }
     }    
     
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jurassic · Jul 18, 2013 at 02:11 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jurassic · Jul 18, 2013 at 02:41 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

NullRefenceException Using Arrays 2 Answers

2D jump with Raycast 1 Answer

How Do I Check if another key was pushed while one key was being held down? 0 Answers

Could someone explain this code for me please? 1 Answer

Character facing the position of mouse cursor (2d platformer),Sprite changing based on mouse position 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges