Question by
samuraifan · Nov 23, 2015 at 02:44 AM ·
mousemouseclickcodepage
Small issue with code, Help needed!
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 as well) 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, it wasn't what I intended.
Where have I gone wrong?
void PlayerMover()
{
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(new Vector2(0f, playerJump));
}
}
if (one_click)
{
if ((Time.time - timer_for_double_click) > jumpDelay) {
one_click = false;
onGround = false;
sprintCountDown = 1f;
}
}
}
Comment