- Home /
Force GetMouseButtonUp
I've got an action in my game that has you clicking and holding and then something fires off on releasing, but I want to have it on a timer so that it automatically releases after X amount of time.
The problem is even doing things like making a bool part of the mousebuttondown if statement and then disabling it after a certain time doesn't stop the current mousebuttondown. I need a way to force end the current action, basically forcing mousebuttonup without the input of the player. Any ideas?
Hello, could you let me know if my solution worked for you? I'd like to know whether this was helpful for you or not
Answer by didicayu · Nov 23, 2021 at 11:27 PM
You actually also have the function GetMouseButton( ) that detects when the mouse is being held and not just when it is pressed or released, you could make a timer so that the action only fires off after a certain amount of time, here's an example:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseButton : MonoBehaviour
{
public float delay = 10f;
float timepassed = 0;
// Update is called once per frame
void Update()
{
if(Input.GetMouseButton(0) && timepassed > delay){
Debug.Log("fire");
timepassed = 0;
}
timepassed += Time.deltaTime;
}
}
where the delay is the time in the number of seconds you want to wait for before doing the action again. Regardless of whether the user is having the mouse pressed all the time or not, the action will not be fired until the indicated time has passed
Hm, this puts a delay on the initial press of the button, but I need an action to happen after a certain time that disrupts the current GetMouseButton situation, basically the action for clicking and holding the mouse holds a ball in place and then releasing it launches the ball, so if I force launch the ball but the player is still holding the button it may "launch" but immediately it goes back to being static because the mouse button is still being held.
Your answer
Follow this Question
Related Questions
Is it possible to do 'click to move' in the free version of Unity 1 Answer
Right click not working in the editor with sub windows. 0 Answers
"Click" a GUI Button using only joystick input 1 Answer
Input.GetMouseButtonDown(1): do the primary and secondary buttons work differently? 1 Answer
setting up mouse button 2 Answers