Launch ball after too long waiting
Hello, I would like to add a function to my game, so if the player holds a left mouse button for too long (let's assume for 5 seconds) the ball is fired anyway. I've added float timer, and float hold duration but I must have done something wrong because my game is working, but it is not counting time, so it makes it too easy to play. I've tried solution from this topic link text but it didn't work. Code:
if (Input.GetButtonDown("Fire1"))//mouse button was clicked
{
if (hit.collider != null)
{
if (hit.collider.transform.root.name == name)
{
trajectoryVisible = true;
}
}
}
//the mouse button was released or trajectory path touched screen edge
if ((Input.GetButtonUp("Fire1") || (paraboleReachedEndOfScreen && Input.GetButton("Fire1"))) && !isClicked && trajectoryVisible)
{
trajectoryVisible = false;
isClicked = true;
GameStatus.ball.ballRB.constraints = RigidbodyConstraints2D.FreezeRotation;
ballRB.velocity = shotForce;
foreach (var dot in dots)
dot.transform.position = Vector3.zero;
}
Answer by MagMaffin · Apr 07, 2020 at 11:44 AM
I've managed to do the timer, so the ball is launched after 5 seconds, but now it works like that: the ball was pressed too long and is fired, after the score, or after game over it goes in the same way as it was before and it is fired immediately. What might be the cause?
paraboleReachedEndOfScreen = lastDotTransform.position.x <= -GameStatus.MainCamera.x / 2 || lastDotTransform.position.x >= GameStatus.MainCamera.x / 2;
timeRunsOut = Time.time - timer > holdDuration;
ballClick.SetActive(!isClicked);
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if (Input.GetButtonDown("Fire1"))//mouse button was clicked
{
timer = Time.time;
if (hit.collider != null)
{
if (hit.collider.transform.root.name == name)
{
trajectoryVisible = true;
}
}
}
//the mouse button was released or trajectory path touched screen edge
if (((Input.GetButtonUp("Fire1") || (paraboleReachedEndOfScreen && Input.GetButton("Fire1"))) && !isClicked && trajectoryVisible) || (timeRunsOut && !isClicked && trajectoryVisible))
{
trajectoryVisible = false;
isClicked = true;
GameStatus.ball.ballRB.constraints = RigidbodyConstraints2D.FreezeRotation;
ballRB.velocity = shotForce;
foreach (var dot in dots)
dot.transform.position = Vector3.zero;
}
Your answer
Follow this Question
Related Questions
Button Press and Hold, instead of Click? 0 Answers
how to hold button longer, the more powerful shot ? 0 Answers
Trouble Navigating Pause Menu.... 0 Answers
How to get touch's positon when I hold the button?? 2 Answers
Need to press key 2 times to shoot 0 Answers