Basically I have the player moving across the screen when the game is started. The plan is to create a basic game that uses basic controls (tap&hold to sprint and tap-tap to jump, may add a double jump) to complete a platform style game. I have a sprint countdown that triggered when the screen is pressed and held for longer than 1 second BUT in some instances the player will continue sprinting even though the screen is no longer pressed. It may be a minor problem but wasn't what I intended. What did I overlook? voidPlayerMover() { if(gameAlreadyStart ==true){ Vector3 pos = transform.position; pos.x += moveSpeed *Time.deltaTime; transform.position = pos; anim.SetBool("running",true); } if(Input.GetMouseButton(0)){ sprintCountDown -=Time.deltaTime; if(sprintCountDown <0){ anim.SetBool("sprinting",true); } } if(Input.GetMouseButtonUp(0)){ anim.SetBool("sprinting",false); sprintCountDown =1f; } if(Input.GetMouseButtonDown(0)){ if(!one_click){ one_click =true; Debug.Log("click"); timer_for_double_click =Time.time; } else{ one_click =false; Debug.Log("double click"); if(onGround ==true) GetComponent<Rigidbody2D>().AddForce(newVector2(0f, playerJump)); }} if(one_click) { if((Time.time - timer_for_double_click)> jumpDelay){ one_click =false; onGround =false; sprintCountDown =1f; } }}
Try putting "if(Input.GetMouseButtonUp(0))" after the codes for the sprinting. This means that when you release the tap, it will trigger an action, and you can put some codes to make it trigger by making the "sprint = false;". I'm not sure though, I'm just a beginner. Inform me if this works
try with else instead of buttonUp... and yes please do use code tags... :-( Code (CSharp): if(Input.GetMouseButton(0)){ sprintCountDown -=Time.deltaTime; if(sprintCountDown <0){ anim.SetBool("sprinting",true); } } else { anim.SetBool("sprinting",false); sprintCountDown =1f; }
@Argonite Did what you suggested moreorless. I put the mouseup code in my Update function after the player movement function is called. Worked like a charm. Thanks