How do I detect when a mouse is being held, and when it is release?
if(Input.GetMouseButtonDown(0)){
Debug.Log("Held");
}
else{
Debug.Log("Not held");
}
For some reason, that doesn’t work. It always displays as not held.
You want to use Input.GetMouseButton(0), which registers every frame the mouse is down, opposed to Input.GetMouseButtonDown(0), which only registers on the first frame the user clicks.
Note that that was the method I linked you to ![]()
… I don’t get it…
Stop looking at GetMouseButtonDown, since it’s not the method you want.
You can use
if (Input.GetMouseButtonDown(0))
print ("Pressed");
else if (Input.GetMouseButtonUp(0))
print ("Released");
or else
if (Input.GetMouseButton(0))
print ("Pressed");
else
print ("Not pressed");
GetMouseButton() Returns true for every frame that the mouse is being pressed.
GetMouseButtonDown() (or GetMouseButtonUp() ) only return true during the ONE frame that the mouse button was pressed (or released).
Make sense?
Thanks so much!
thank u it solve my purpose
Hi,
I have a tower defense game where I click a button for placing a tower.
Then an image of the mouse is following my mouse around the screen showing the range as well.
When I click (GetMouseButtonDown(0)) the tower is placed.
That’s nice but as I want to port the game over to Wii U as well I want to change that behavior.
Right now the tower is placed instantly, even if I don’t release the mouse button.
But I want the tower to be placed only then when I release the mouse button.
Because the nicer hover icon is not shown on the Wii U until I touch the gamepad’s screen but when I do it’s instantly placed. Instead I want to touch and drag my finger around with the tower showing its range and place it where I release my finger.
if (Input.GetMouseButtonDown(0))
{
PlaceTower();
}
That’s my code right now for calling the PlaceTower function. Simple as that.
I tried something like this but it didn’t work:
if (Input.GetMouseButton(0))
{
if (Input.GetMouseButtonUp(0))
{
PlaceTower();
}
}
Any suggestions?
It is probably not the best way to do it, but it work:
private bool is_clicking = false;
if(Input.GetMouseButton(0))
{
is_clicking = true;
}
if (Input.GetMouseButtonUp(0) && is_clicking)
{
PlaceTower();
is_clicking = false;
}
You have to know the order of mouse events first.
Input.GetMouseButtonDown(0)
Input.GetMouseButton(0)
Input.GetMouseButtonUp(0)
so, you can track your logic by adding a boolean in mouseDown and check it in mouseButton and finally turn it off in mouseUp.
something like this:
if (Input.GetMouseButtonDown (0)) {
is_down = true;
}
if (Input.GetMouseButton (0) && is_down) {
// Do Something
}
if (Input.GetMouseButtonUp (0)) {
is_down = false;
}
You can also use:
void OnMouseDrag()
{
///anything you want to do
}
and right button?
if (Input.GetMouseButton(1))
{
Debug.Log("Pressed right click.");
}
From: https://docs.unity3d.com/ScriptReference/Input.GetMouseButton.html
void OnMouseDown() {
if(Input.GetMouseButton(0)) {
//code for left button
}
if(Input.GetMouseButton(1) ){
//code for right button
}
if(Input.GetMouseButton(2)) {
//code for midle button
}}
Whilst it’s appreciated you trying to help, this thread was already answered and unfortunately your post isn’t quite correct either. Your post shows you how to determine “which” button was pressed but it only works when the mouse was pressed over a Collider which isn’t specifically what the thread is about.
Also, this thread is 11 years old. I think it’s time it’s locked.